jonaHill87 Posted April 29, 2008 Share Posted April 29, 2008 Hi all, I just need a bit of help trying to edit a string. I have random values that I'm pulling out of a database. The values are numbers with decimal points. For example 23.8604 or 345.7772. I just want all of the numbers before the decimal place and the two numbers after. So instead of 345.7772, I want to display 345.77. I'm not sure what string functions I can use to do this. Any help would be appreciated. Link to comment https://forums.phpfreaks.com/topic/103433-solved-edit-a-string/ Share on other sites More sharing options...
p2grace Posted April 29, 2008 Share Posted April 29, 2008 number_format($string,2); http://us2.php.net/manual/en/function.number-format.php Link to comment https://forums.phpfreaks.com/topic/103433-solved-edit-a-string/#findComment-529650 Share on other sites More sharing options...
Fadion Posted April 29, 2008 Share Posted April 29, 2008 number_format() will do it, but another approach would be using the way more powerfull sprintf(): <?php $nr = 1524.57422; echo sprintf('%.2f', $nr); ?> Link to comment https://forums.phpfreaks.com/topic/103433-solved-edit-a-string/#findComment-529664 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.