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);
Related
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
Am having an issue where PHP expands the float value e.g. 241.09 becomes 241.0899999999. The issue is, the expanded value is included in a signature, thus the expansion is causing my signature to have a mismatch when matching with the actual data which has the original unexpanded form. How do I prevent the rounding off? My code section is as below:
round(floatval($resultParams["AvailableFunds"]), 2)
Somehow, even after applying the round function, the expansion still occurs. How do I prevent that??
It's caused by the PHP Floating point precision:
Floating point numbers have limited precision.
A solution may be to use only integer, e.g. save the float 123.45 as 12345 and when you need to use display it divide it by 100 and pass it to the number_format function:
$myNumView = number_format($myNum/100, 2, '.', '')+0;
By doing this, number_format returns a string formatted as 1234567.89, then by doing +0 PHP converts it to float (due to the PHP Type Juggling) and removes the rightmost 0 in the decimal part of the number:
$a = '35';
$b = '-34.99';
echo $myNum = ($a + $b);
echo '<br>';
echo $myNumView = number_format($myNum, 4, '.', '')+0;
returns:
0.009999999999998
0.01
And also, why does you get AvailableFunds from a string with floatval? It seems that AvailableFunds is a string containing the amount of fund and other text. I think this is a bad implementation on how saving the fund amount. It's better to save the value as is in a dedicated field as float.
This question already has answers here:
Unformat money when parsing in PHP
(8 answers)
Closed 7 years ago.
How to convert a string like "3,2563" to "3.2563",
$number = "3,2563" ;
setlocale(LC_MONETARY,"en_US");
echo money_format("%.4n", $number);
or
$number = number_format($number,4,".","");
Both examples output just 3.0000
The string "3,2563" is not a number, thus - it cannot be used as such.
It can easily be converted to a float number, using PHP function str_replace and type casting.
$number = "3,2563";
$number = (float)str_replace(",", ".", $number); // returns (float) 3.2563
// Do whatever you want to do. Now $number is a float.
Using str_replace, the , is replaced with a .
Note that the decimals separator can vary, depending on your PHP configuration.
"3,2563" is a string, you're trying to display a string as a number, that's not possible.
You can replace , with . before changing its type:
$number = "3,2563";
$number = str_replace(',', '.', $number); // get "3.2563"
$number = (float) $number; // get a floating number
setlocale(LC_MONETARY,"en_US");
echo money_format("%.4n", $number); // shows "3.2563"
echo money_format("%.2n", $number); // shows "3.26"
You're using a string ill-formatted for the desired use-case and existing logic you have in your code - i.e. '3,2563'. Let me be more clear. In some countries, a comma is used instead of a decimal to demarcate a whole unit of some currency and fractional units of some currency. In other cases, the comma and decimals indicate a thousand whole unit of some currency. It depends on what you're aiming for which isn't clear based on the example you gave... plus, I'm not aware of every monetary syntax convention.
In any event, the general procedure you want to employ is to remove all the commas or to normalize the number (for example use 32563 instead of 3,2563 if you're going for whole units), do your operations, and then reapply the convention (I assume that they're monetary conventions) that you want at the end. If you just want to replace the comma with a decimal - you can still use str_replace() to accomplish that as well. Build a function or class to do that so you can reuse that code for use with other similar problems.
My recommendation, though it wasn't explicit, is to simply use some str_replace() logic to generate a normalized/indexed number.
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
I'm hoping to do some math operations on numbers being represented as strings. So far, I haven't found anything that could do multiplication.
Thanks!
You need of the bcmul() method. You too can take a look on BC Math functions.
Example:
echo bcmul('2.123456', '4.7891011', 6); // 6 is the precision
You mean bcmul?
http://www.php.net/manual/en/function.bcmul.php
bcmul — Multiply two arbitrary precision number
string bcmul ( string $left_operand , string $right_operand [, int $scale ] )
Multiply the left_operand by the right_operand.
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.