barkster Posted September 11, 2006 Share Posted September 11, 2006 I'm trying to format a number to 2 decimal places like currency but when I apply this to a number with a comman in it like 12,233 I get 12.00? I obviously don't understand how this is working since a comma isn't period and it is rounding. How can I insure that I'm always getting a two decimal place number. I used money format but it was dropping the decimal places. I'm looking for a number like 12,233.00 above. Thanks Link to comment https://forums.phpfreaks.com/topic/20386-formatting-number/ Share on other sites More sharing options...
wildteen88 Posted September 11, 2006 Share Posted September 11, 2006 use number format: like this:[code=php:0]$number = '12,589';// remove any spaces/commas$number = str_replace(array(',', ' '), '', $number);$number2 = number_format($number, 2, '.', ',');echo $number2;[/code]European countries such as france, spain etc use a comma (,) as a decimal seperator rather than a period (.). SO number format is rounding the number. What you should do is remove any commas/spaces from the number. Then pass the number into the format_number function. Link to comment https://forums.phpfreaks.com/topic/20386-formatting-number/#findComment-89778 Share on other sites More sharing options...
obsidian Posted September 11, 2006 Share Posted September 11, 2006 you need to strip the comma out before you run your format on it. in PHP, numbers [b]do not[/b] contain commas, so you've got to try to get it down to the digits only if possible:[code]<?php$num = "12,233";$num = preg_replace('|,|', '', $num);echo number_format($num, 2);?>[/code]hope that helps Link to comment https://forums.phpfreaks.com/topic/20386-formatting-number/#findComment-89781 Share on other sites More sharing options...
barkster Posted September 11, 2006 Author Share Posted September 11, 2006 that explains it, thanks Link to comment https://forums.phpfreaks.com/topic/20386-formatting-number/#findComment-89784 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.