Reputation: 3733
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
Upvotes: 2
Views: 2523
Reputation: 5119
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.
Upvotes: 3
Reputation: 373
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.
Upvotes: 0
Reputation: 416
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)
Upvotes: 1