smithmr8 Posted July 12, 2008 Share Posted July 12, 2008 Hi, I don't know how to format a number, e.g. 4.444444444, to a certain number of decimal places. In this case, I need it to be '2'. So, 4.44444444 would become 4.44 ect.. I was wondering whether someone would be able to help me out. Regards, Luke Link to comment https://forums.phpfreaks.com/topic/114399-formatting-a-number-to-2-decimal-places/ Share on other sites More sharing options...
vikramjeet.singla Posted July 12, 2008 Share Posted July 12, 2008 refer this... http://php.net/number_format Link to comment https://forums.phpfreaks.com/topic/114399-formatting-a-number-to-2-decimal-places/#findComment-588292 Share on other sites More sharing options...
sureshmaharana Posted July 14, 2008 Share Posted July 14, 2008 Try This: $number = 456.58795 $format_number = number_format($number, 2, '.', ''); Link to comment https://forums.phpfreaks.com/topic/114399-formatting-a-number-to-2-decimal-places/#findComment-589533 Share on other sites More sharing options...
MasterACE14 Posted July 14, 2008 Share Posted July 14, 2008 or... round(); function. look it up on php.net Regards ACE Link to comment https://forums.phpfreaks.com/topic/114399-formatting-a-number-to-2-decimal-places/#findComment-589566 Share on other sites More sharing options...
vikramjeet.singla Posted July 14, 2008 Share Posted July 14, 2008 Master... i think that is for rounding off the number... $a = 1.234446; echo round($a); // 1.23445 not restricting number to two decimal points.... Link to comment https://forums.phpfreaks.com/topic/114399-formatting-a-number-to-2-decimal-places/#findComment-589569 Share on other sites More sharing options...
MatthewJ Posted July 14, 2008 Share Posted July 14, 2008 You can round with something like $num = 1.45678 round($num, 2); And that will round to two decimals, but rounding is usually not what people want. Link to comment https://forums.phpfreaks.com/topic/114399-formatting-a-number-to-2-decimal-places/#findComment-589574 Share on other sites More sharing options...
vikramjeet.singla Posted July 15, 2008 Share Posted July 15, 2008 you are correct Matthew... but that can be done with number_format..... please refer to link below: http://php.net/number_format Link to comment https://forums.phpfreaks.com/topic/114399-formatting-a-number-to-2-decimal-places/#findComment-590397 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.