I'm trying to convert a string number like 14,767 or 96,812.10 or 6,780,766.50 but if I use
(double)$fildOfDataBase or (float)$fildOfDataBase this put a integer format as 14 o 96 and I don't know why, so my question is how can i convert a string to a number format, hope some one can help me I'm using laravel 8
You need to remove the thousands separator first
(float)str_replace(',', '', $fildOfDataBase);
Another more "strict" way to do it is to use numfmt_parse
//in your case use the en_EN format
$fmt = numfmt_create( 'en_EN', NumberFormatter::DECIMAL );
numfmt_parse($fmt, $fildOfDataBase);
Related
I would like to know that how can i format my number including comma and dot to decimal. Thanks. In my database , my amount column is decimal type with (16,8) . after i formatting i want to save to database my number . For example
my input number 10,000.1111 and want to save as a 10000.1111 to my database. Thanks.
You could use this to format the string to a float, and remove the commas.
(float) str_replace(',', '', '10,000.1111')
Typecasting (float) will force the outputted variable as a float. And str_replace() will remove the commas from the inputted variable.
$input = '10,000.1111';
$model->column = (float) str_replace(',', '', $input);
$model->save()
is there any way or method to generate fake string using laravel faker ?
like in laravel we generate string upto 20 chars..
str_random(20);
Faker offers a couple of methods that let you replace placeholders in a given string with random characters:
lexify - takes given string and replaces ? with random letters
asciify - takes given string and replaces * with random ascii characters
numerify - takes given string and replaces # with random digits
bothify - combines the lexify and numerify
You could try to use one of them, depending on the requirements you have for that random string you need. asciify uses the largest set of characters as replacement so using that one makes most sense.
The following will give you a random string of 20 ascii characters:
$faker->asciify('********************')
Alternate for generate string without special chars.
$faker->regexify('[A-Za-z0-9]{20}')
$faker->text($maxNbChars = 50);
$faker->text()
// generates 50 char by default: "Aut quo omnis placeat eos omnis eos."
$faker->text(10);
// generates 10 char by default: "Labore."
All texts seems to be one or more latin pseudo-sentences with spaces and always a dot in the end (of each sentence).
uze Faker\Provider\en_US\Text
<?php
realText($maxNbChars = 200, $indexSize = 2) // "And yet I wish you could manage it?) 'And what are they made of?' Alice asked in a shrill, passionate voice. 'Would YOU like cats if you were never even spoke to Time!' 'Perhaps not,' Alice replied."
I have a file details.txt in which data stored is in this format
"571955NandhithaF1975-12-222011-12-06Mumbai"
Columns are first six digit unique id ,
name , (M/F) Gender , dob,joining date , and location
i have to separate this in six columns using comma delimiter !!
Please help me in this problem
Pass each line into a regex function which contains the below logic :
String expression = "571955NandhithaF1975-12-222011-12-06Mumbai";
Pattern pattern = Pattern
.compile("([0-9]{6})([a-zA-Z]+)([M|F])([0-9]{4}-[0-9]{2}-[0-9]{2})([0-9]{4}-[0-9]{2}-[0-9]{2})([a-zA-Z0-9]+)");
Matcher matcher = pattern.matcher(expression);
if (matcher.find()) {
//System.out.println(matcher.group());
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
System.out.println(matcher.group(3));
System.out.println(matcher.group(4));
System.out.println(matcher.group(5));
System.out.println(matcher.group(6));
}
output:
571955
Nandhitha
F
1975-12-22
2011-12-06
Mumbai
571955NandhithaF1975-12-222011-12-06Mumbai
To split this type of data, we have to use String Functions in java in the mapper class under map method.
You can use substring(beginindex,endindex) method to get Id of from the string, its
like string id[6]=substring(0,5) which returns 6 digit string that is ID.(As ID is Fixed length we take 6)
You can use substring(beginindex) to get remaining string.
Next on wards you have to use REGXP in java.. along with split(regexp) to get the name, gender, dob, doj, loc. But definitely some workout with java takes place.
go through this link for String functions in java.
Hope this post may help.
If any suggestions or modifications to the same are also accepted :)
I want to check if a string contains only '*' and treat replace it with blank.
For eg:
Scenario 1: if the string contains '**abc*%#' or 'xyz*$#*!' then I need to retain their values as is.
Scenario 2: if the string contains values like '****' or '*' or any combination where only this special character is present in the string then I need an output of ''.
Please suggest what would be the best way to go about this.
Thanks.
You can achieve this by getting the length of the string and replacing the * with '' and then subtract both of them. If the length is 0 then it contains all *
Example
int count = line.length() - line.replace(".", "").length();
Hope this helps.
-Help :)
I need correct sample of using decimal format and decimal rounding to closest integer.
Sample1: I have number 123.345,51 result need to be 123.346,00
Sample2: I have number 123.345,49 result need to be 123.345,00
I found samples but they all use format with comma first 123,345.00 I need in first place point like 123.345,00
Tried with culture info but did not success ..
Sample code:
var amount = 123.345,77;
var cultureInfo = CultureInfo.GetCultureInfo("da-DK");
var formattedAmount = String.Format(cultureInfo, "{0:C}", amount);
When I need to convert formattedAmount to decimal its breaks... I am converting for Rounding values after ,
Solution is to enable users to input in format they want, like 123123,55 then use .Replace(",", ".") to replace , colon character with dot, after that made Decimal.Round and all problem are solved!