morlan Posted August 14, 2007 Share Posted August 14, 2007 Hi folks, Im trying to add a decimal point before the last two digits of a numeric string using number_format, but it doesnt seem have the desired effect. I've tried using number_format($number,2), but this adds a decimal point to the end. This is what I'm trying to achieve 1000 = 10.00 10000 = 100.00 and so on. Link to comment https://forums.phpfreaks.com/topic/64886-solved-number_format-question/ Share on other sites More sharing options...
wildteen88 Posted August 14, 2007 Share Posted August 14, 2007 number_format will only place the decimal point in the specified place if you pass it a float: $num = '123.456'; echo number_format($num, 2); // result: 123.46 If you pass it a whole number (integer) then it'll add .00 to the end of the number. If you want to always place the decimal before the last two digits of a number you'll have to do it manully: <?php $num = '10000'; // get last two digits of number $l2d = substr($num, -2, 2); // place decimal before the last two digits of the number $new_num = substr_replace($num, '.', -2, 2) . $l2d; echo $num . '<br />' . $new_num; ?> Link to comment https://forums.phpfreaks.com/topic/64886-solved-number_format-question/#findComment-323769 Share on other sites More sharing options...
micah1701 Posted August 14, 2007 Share Posted August 14, 2007 what if you divide by 10 first? $number = $number / 10; number_format($number,2); Link to comment https://forums.phpfreaks.com/topic/64886-solved-number_format-question/#findComment-323770 Share on other sites More sharing options...
morlan Posted August 14, 2007 Author Share Posted August 14, 2007 <snip> I was actually just reading my manual on substr and substr_replace funtions. Thanks for the good explanation. Link to comment https://forums.phpfreaks.com/topic/64886-solved-number_format-question/#findComment-323785 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.