How to show number in readable format - php

I want to know how can I add comma's to numbers. I want to change number 5,272,945.31 to 52,72,945.31
Is it possible with a php function?
I have try this code:
number_format($amount,2);

The value assigned to $amount isn't formatted correctly, however, number_format doesn't like , so strip these out before passing to number_format.
number_format(str_replace(',', '', $amount), 2);
Example: https://3v4l.org/dPAEp

Related

How to format number to currency but don't show the symbol?

What i'm trying to achieve is converting a number to currency format to add decimals and commas whenever needed , but i don't want it to show the currency symbol.
I used to use number_format but that's depreciated in favor of the newer function.
I tried this:
$val = "23";
$fmt = numfmt_create( 'en', NumberFormatter::CURRENCY);
// i tried the following
$num = numfmt_format_currency($fmt, $val); // omitted the 3rd parameter but caused error
$num = numfmt_format_currency($fmt, $val,null); // tried to null or blank the 3rd param but im getting a weird symbol / prefix
EDIT: correction, i meant money_format is depreciated, not number_format . sorry
To keep it easy MultiSuperFreaks answer is the most common. The function number_format is not deprecated in any way.
If you want to use the intl extension and the NumberFormatter class, you can go like ...
$formatter = new NumberFormatter('en_GB', NumberFormatter::DECIMAL);
$formatter->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, 2);
var_dump($formatter->format(23)); // string(5) "23.00"
Just do not use the NumberFormatter::CURRENCY type. Instead use the NumberFormatter::DECIMAL type when instanciating the NumberFormatter class and then set the attribute for minimal 2 fraction digits.
I all you want is to add two decimals to every number, you can use number_format($val, 2) to get a formatted number without any symbol.
EDIT: Number format in the standard PHP library is not deprecated (https://www.php.net/manual/en/function.number-format)
You can use preg_replace for doing it, just like in the following example:
$string = '#15.32';
$pattern = '/([^0-9]*)(\d*)(.*)/';
$replacement = '${2}${3}';
echo preg_replace($pattern, $replacement, $string);
The output will be: 15:32. The idea is that everything up to the first number will be discarded, leaving you with all the rest. So in your example, only the number will stay.

PHP replace characters "," in a string number to "." [duplicate]

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.

PHP - number_format issues

number_format stupidly rounds numbers by default. Is there just a simple way to turn off the rounding? I'm working with randomly generated numbers, and I could get things like...
42533 * .003 = 127.599 or,
42533 * .03 = 1275.99 or,
42533 * .3 = 12759.9
I need number_format (or something else) to express the result in traditional U.S. format (with a comma separating the thousands) and not round the decimal.
Any ideas?
The second argument of number_format is the number of decimals in the number. The easiest way to find that out is probably to treat it as a string (as per this question). Traditional US format is default behaviour, so you don't need to specify remaining arguments.
$num_decimals = strlen(substr(strrchr($number, "."), 1));
$formatted_number = number_format($number, $num_decimals);
If anyone's interested in this, I've written a little function to get around this problem.
With credit to Joel Hinz above, I came up with...
function number_format_with_decimals($value, $round_to){
//$value is the decimal number you want to show with number_format.
//$round_to is the deicimal place value you want to round to.
$round_to_decimals = strlen(substr(strrchr($value, "."), $round_to));
$ans = number_format($value, $round_to);
return $ans;
}
I tried these solutions, but what ended up being my answer was this:
$output=money_format('%!i', $value);
This gives you something like 3,234.90 instead of 3,234 or 3,234.9

Format a float to two decimal places

I want to format a float value I have to two decimal places.
For example if I have a float for a price of 5.2, I want it to be formatted as 5.20.
Try number_format:
echo number_format("5.2",2)
You'll want to use printf or sprintf. You can read more about it in the php docs.
If you're simply trying to ensure a number is displayed with two decimal places, you could also use number_format, or (if this is a currency value) money_format.
e.g.: echo number_format('5.2', 2);
Try this:
number_format((float)$foo, 2, '.', '');

convert number to decimal in php

I have a number stored in a variable. The number is pulled from the database as 9900..I need to convert this number to 99.00 in order to display to a customer in HTML.
I was wondering how can I achieve this in php.
Thanks.
$priceInCents = 9900;
$priceInDollars = $priceInCents / 100;
And then you can use round() or number_format() as you please.
You can use the number_format() function for that:
echo number_format(9900/100);
You can do this by either using money_format or number_format, if you are going to display it as a price you should look at money_format, otherwise, number_format is the way to go.

Categories