pocobueno1388 Posted July 15, 2007 Share Posted July 15, 2007 I have a number that looks like this: 0.014 I am trying to: Move the decimal two places to the right and delete any 0's at the beginning of the number. How could I accomplish this? This is as far as I got: <?php $num = "0.014"; //find where the decimal is in the number $find_decimal = strpos($num, "."); //where the decimal should be [2 places to the right] $decimal = $find_decimal+2; ?> Now I'm just lost of what to do....any help is greatly appreciated, Thanks Link to comment https://forums.phpfreaks.com/topic/60097-solved-moving-the-decimal-two-places-over/ Share on other sites More sharing options...
redarrow Posted July 15, 2007 Share Posted July 15, 2007 try this m8 http://uk2.php.net/number_format Link to comment https://forums.phpfreaks.com/topic/60097-solved-moving-the-decimal-two-places-over/#findComment-298912 Share on other sites More sharing options...
xyn Posted July 15, 2007 Share Posted July 15, 2007 $num = 0.014; number_format($num, 0, "."); Link to comment https://forums.phpfreaks.com/topic/60097-solved-moving-the-decimal-two-places-over/#findComment-298914 Share on other sites More sharing options...
pocobueno1388 Posted July 15, 2007 Author Share Posted July 15, 2007 $num = 0.014; number_format($num, 0, "."); That gives me an error: Warning: Wrong parameter count for number_format() in ..... on line 4 Link to comment https://forums.phpfreaks.com/topic/60097-solved-moving-the-decimal-two-places-over/#findComment-298917 Share on other sites More sharing options...
pocobueno1388 Posted July 15, 2007 Author Share Posted July 15, 2007 I'm not sure that number_format() will move the decimal... Link to comment https://forums.phpfreaks.com/topic/60097-solved-moving-the-decimal-two-places-over/#findComment-298919 Share on other sites More sharing options...
pocobueno1388 Posted July 15, 2007 Author Share Posted July 15, 2007 Well, I just found a nice function on PHPfreaks: <?php function move_decimal($number,$direction,$places = 1) { if($direction == 0) { $number = $number * pow(10, ($places * -1)); } if($direction == 1) { $number = $number * pow(10, $places); } return $number; } ?> So let me try this, and I will report back if I got everything working. Link to comment https://forums.phpfreaks.com/topic/60097-solved-moving-the-decimal-two-places-over/#findComment-298923 Share on other sites More sharing options...
xyn Posted July 15, 2007 Share Posted July 15, 2007 number_format() will move the decimal. i used it in my rating system. Link to comment https://forums.phpfreaks.com/topic/60097-solved-moving-the-decimal-two-places-over/#findComment-298926 Share on other sites More sharing options...
pocobueno1388 Posted July 15, 2007 Author Share Posted July 15, 2007 Well the function worked perfect =] Thanks for the help ^^ Link to comment https://forums.phpfreaks.com/topic/60097-solved-moving-the-decimal-two-places-over/#findComment-298927 Share on other sites More sharing options...
redarrow Posted July 15, 2007 Share Posted July 15, 2007 the number format function is correct ok. <?php $number = 1234.5678; $english_format_number = number_format($number, 2, '.', ''); ECHO $english_format_number; ?> Link to comment https://forums.phpfreaks.com/topic/60097-solved-moving-the-decimal-two-places-over/#findComment-298931 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.