开发者

Commercial round with comma and point

开发者 https://www.devze.com 2023-02-25 12:56 出处:网络
I have used this code : $result = number_format(round(15.5,开发者_StackOverflow中文版 1), 2); Written in my old post :

I have used this code :

$result = number_format(round(15.5,开发者_StackOverflow中文版 1), 2);

Written in my old post : PHP - Commercial round

I have now problems with the values like : 1,597.30

I get the price as 1.00

How can I do to get the make the round and get the good price format with the prices that have number with thousands.


Never store a formatted number.

Always store it in its original format: 1597.30 This is the only format that works for calculations and formatting.

Use number_format() only for outputting the number.


You could solve this issue using the following code:

$price       = '1,597.30';
$new_price   = floatval(str_replace(',', '', $price));
$result      = number_format(round($new_price, 1), 2);


Remove comma and all other formating (except the dot) from your numbers, otherwise comma will be used as decimal separator.

0

精彩评论

暂无评论...
验证码 换一张
取 消