Php format price with commas
In my database I have values like Show
I have to multiply these values with thousand and then format the result as price(comma separated) 256.23 x 1000 = 256230 I want to show this as 256,230 200.33 x 1000 = 200330 I want this as 200,330 89.33 x 1000 = 89330 I want this as 89,330 Currently I am using formula
But how to format this, I've no idea. asked Jul 25, 2012 at 12:37
2 You are looking for the number_format function.
This function accepts either one, two, or four parameters (not three): If only one parameter is given, number will be formatted without decimals, but with a comma (",") between every group of thousands. If two parameters are given, number will be formatted with decimals decimals with a dot (".") in front, and a comma (",") between every group of thousands. If all four parameters are given, number will be formatted with decimals decimals, dec_point instead of a dot (".") before the decimals and thousands_sep instead of a comma (",") between every group of thousands. answered Jul 25, 2012 at 12:38
FluffehFluffeh 32.8k16 gold badges66 silver badges80 bronze badges
answered Jul 25, 2012 at 13:11
ObhasoObhaso 4311 gold badge4 silver badges14 bronze badges 0 Check number_format, here is an example
answered Jul 25, 2012 at 12:41
FabioCostaFabioCosta 2,9494 gold badges27 silver badges49 bronze badges 1 The answers above do not account for decimals or rounding, this may be helpful to people who need to worry about decimals: Examples: show no decimals use spaces instead of commas, and print with decimal and commas:
Output:
answered Jun 20, 2018 at 16:42
Mike QMike Q 6,0384 gold badges50 silver badges57 bronze badges 1
answered Apr 21, 2014 at 9:36
DJ MHADJ MHA 5983 silver badges18 bronze badges Here is the custom function to convert price into Indian Price format using PHP 7+
answered Aug 29, 2019 at 12:49
How to format number with comma in PHP?The number_format() function rounds numbers and adds commas as a thousands separator.. number_format($n) rounds $n to the nearest whole number and adds commas in between thousands. ... . number_format($n,$p) rounds $n to $p decimal places, adding commas between thousands.. How do you write a comma with a price?Some countries use a comma (,) instead of a decimal to indicate that separation.. 500 or 500,00 or 500.00 = five hundred dollars and no cents.. 500,15 or 500.15 = five hundred dollars and fifteen cents.. 500,150 or 500.150 or 500,150.00 or 500.150,00 = five hundred thousand, one hundred fifty dollars and no cents.. How to number format in PHP?The number_format() function is an inbuilt function in PHP which is used to format a number with grouped thousands. It returns the formatted number on success otherwise it gives E_WARNING on failure.
How should comma separated values be stored in PHP?The comma separated list can be created by using implode() function. The implode() is a builtin function in PHP and is used to join the elements of an array. implode() is an alias for PHP | join() function and works exactly same as that of join() function.
|