Converting this number to human readable form? - php

I have a database that is ported from an XLS file. My numbers are in this form
3.41002E+13
But the readable form should be : 34100224263318
How can I convert the first form to the second?

Use number_format()
string number_format(float $number, int $decimals = 0, string $dec_point = '.', string $thousands_sep = ',' )
By default, thousands are seperated by ,, that's why you need to pass all four arguments:
number_format(3.41002E+13, 0, ".", "");
34100200000000
As you can see, you have an additional problem: You lose precision - sort of at least: It was never there in the first place.

You need to use number_format, check it out here

You will never be able to get the exact number (e.g. 34100224263318)
"Ex" stands for 10^x, which is used for numbers with a lot of digits as an approximation of the actual number.
number_format is what you can use to get the approximation in a standard human-readable numerical version (Ex is readable too, but anyway).

3.41002E+13 == 3.41002*10^13 == 3.41002 * 100000000000000
It is just a float type. If you want to achieve this you need to use number_format();
number_format(3.41002E+13) == 34100224263318
As phant0m commented if you use
number_format(1.11E-2) == 0,00111
but with integers you will be fine if you want a dot just use
number_format(1.11E-2, 5, '.', '') == 0.00111

Related

PHP string to numeric conversion with decimal places

I have a string ($maxDeposit) which is a numeric monetary value. So, for example:
123.00
This string is being passed in to jQuery, it needs to be passed in as a numeric data type. I'm achieving this using the following:
$maxDeposit = floatval($maxDeposit);
This loses the last last decimal place however, so my number looks like:
123.0
I have this method of converting the number to two decimal places:
$maxDeposit = sprintf('%0.2f', round($maxDeposit, 2));
However this also converts the number back to a string. Is there a way I can convert the string to a float but keep the last decimal place? Thanks
No, float is a numeric value, and 123.00 is its representation with 2 decimal places. It is responsibility of view layer to format numbers. In your case it is jQuery, e.g. console.log(maxDeposit.toFixed(2)).
I think, You can use floatval/float and number_format.
$maxDeposit = number_format(floatval($maxDeposit), 2);
or
number_format((float)$maxDeposit, 2, '.', '');
http://php.net/manual/pt_BR/function.number-format.php

Formatting numbers with commas PHP [duplicate]

This question already has an answer here:
PHP - number_format and rounding
(1 answer)
Closed 7 years ago.
How do i correctly format numbers with commas. Example
I wand 10000 to be 10,000 I know i can do it using below code
number_format(10000)
Issue is that if is a number with decimals it tends to remove the decimal.
Ex: 10000.50 it will display the number as 10,001
How can i get around this and correctly display numbers with and without decimals.
There is a clear doc for this function:
string number_format ( float $number , int $decimals = 0 , string $dec_point = "." , string $thousands_sep = "," )
http://php.net/manual/de/function.number-format.php
The right way to format numbers using php is to use the in-built function number_format
string number_format ( float $number , int $decimals = 0 , string $dec_point = "." , string $thousands_sep = "," )
There are four parameters you can pass to the function:
$number: which is the number you want to format.
$decimals: It is the number of decimal places that you want to format upto.
$dec_point: It is the character you want to use (if needed) instead of the usual "." used to represent the decimal point.
$thousands_sep : This is the character you want to use to seperate the thousands places with e.g. ",".
Documentation for the function can be found Here
So, In your question, you will replace number_format(10000) with number_format(10000,2)
Hope this helps.
Cheers!
Please use this
number_format(10000.50);
output will come this format 10,001
number_format(10000,3); number_format(10000,2);

Form input strings to float

I have a php form thats capturing data, mostly strings, and submitting it to an XML file for storage.
I would like one of the inputs to to capture a price (float), and then subtract a percentage tied to that users account (stored in mysql), storing both values in the xml file.
the current code looks like this:
$price = $_POST['product_price'];
$our_price = $price - $user['product_fee'];
This obviously won't work because both values are being stored as strings, not floats.
There is an easy solution to this, I'm obviously missing something here.
You could use floatval to parse the strings as floats.
$price = floatval($_POST['product_price']);
$our_price = $price - floatval($user['product_fee']);
Warning: Read the WARNING in the php float page about precision!!!
Update:
To show two decimal points for the floating value, you could use the number_format() function as:
echo number_format(floatval($our_price), 2, '.', '');
but keep in mind that this function again returns a string, which is expected as it is for presentation purposes mostly. If you wanted you could use floatval again on the returned string to parse it as a float:
$new_float = floatval(number_format(floatval($our_price), 2, '.', ''));
Note: For number_format here we also passed 3 optional parameters after the float:
Number 2 -> for the number of decimals needed
A dot (.) which is the decimal separator, and
A comma (,) which is the thousands separator to be used if needed.
PHP is not very strict when it comes to data types. You should be able to subtract two strings (and yes, that is very weird). For example, this works perfectly fine:
$var = '100.00';
$var2 = '20.00';
var_dump($var - $var2);
That will return float(80) as a result.
But if the strings always represent float values, you could typecast them to be sure. Like:
$price = (float) $_POST['product_price'];
By putting (float) in front of the variable, you force $price to be a float value. You can do the same for the fee and then you should be able to subtract them.
PHP is quite tolerable in that way - it will analyze your variables and coerce their type to float automatically:
fike#v-lubuntu:~$ php -r 'var_dump("12.31" - "0.17");'
double(12.14)
As said above, you can use floatval() or type casting ($var = (float) $string) to perform type casting manually. What is 100% must be said is that you should be thousand times aware of what your variables will cast to, since this expression is perfectly valid and will return true:
true == 'true'
To force type checking, you need to use extra equal sign operator:
true === 'true' // false
true !== 'true' // true

number format for calculation

When 10000-100, then result should be 9900.
I tried when I use:-
< ?php
$num1 = number_format(round(10000,1),2);
$num2 = number_format(round(100,1),2);
echo $num1 - $num2;
?>
The above result is -90, that made me realize that the number_format function is not applicable in calculations.
Would there be any way that I can convert a value of number_format (obtained from POST from a previous page) back to numerical value for normal calculation?
To start, the reason is that:
(int) "10,000.00"
resolves to 10 since it stops parsing at the first non-numeric character. Thanks to PHP's weird type system, this is done implicitly when you subtract the strings.
Yes, you can strip out the commas easily:
$unformatted = str_replace(",", "", $formatted);
but it's cleaner to just post the raw numeric value (you can still use number_format for the displayed value).
EDIT: It is good practice to explicitly convert numeric strings (without commas) to float (or int) with either a cast ((int) or (float)) or the function version (intval or floatval).
I don't think you can perform this 10,000.00 -100.00 with the comma in the equation. Just perform the raw arithmetic operation then format the answer.
$num1 = 10000;
$num2 = 100;
echo number_format(round($num1 - $num2,1),2);
This outputs
9,900.00
There is an easier way.
number_format is for fomating output numbers or to round easy numbers.
number_format gives us power to make well fomed rounded numbers, for a better user experience.
For calcualtion and saving Numbers in your MYSQL Database use this.
Save your Numbers in MYSQL always as type DECIMAL not FLOAT. There are lots of bugs if you want to calculate with FLOAT fields.
Than use the english notation.
$number = 1234.56;
// english notation without thousands separator
$english_format_number = number_format($number, 2, '.', ''); //
// 1234.57
And now you can calculate and save it, without any Bugs.
Always Remember yourself, Saving numbers into $var is always a string.
Yeah, you can deifine type, but it doesn't matter in first case, and its to long to explain here.
For more information about number_format see here -> http://php.net/manual/en/function.number-format.php

getting an odd division error with PHP number_format()

my script calculates a number X by dividing two other numbers, A by B.
X=A/B
when i use number_format(A,2) on A before calculating X, i get a very odd number. Actual figures:
1,045.00 / 5 = 0.2
but if i don't use number_format on A before the division, i get the correct answer. Is number_format somehow making A into a non-number?
1045 / 5 = 209
number_format should be used only while pretty printing the number. Its return value should not used in calculation as you did.
Example:
If $A = 1045;
then number_format($A,2) will be 1,045.00 now if you treat 1,045.00 as a number it will be 1 as comma and remaining char will be ignored and 1/2 is 0.5 which you are getting.
You want round(A, 2), not number_format() which is for string representations (hence named "format").
The docs show that number_format returns a string. Have you tried casting the result of number_format() to a numeric type before your mathematical manipulation?
I had similar issues. It could be better if we use number format dec_point and thousand_separator parameters. you could use number_format($number, 2, '.', ''); It will help to remove your thousand separator
number_format makes it into a string with commas between thousands, and the comma will be confusing the divisor into thinking that's the decimels based on your locale.

Categories