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 Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
xyn Posted July 15, 2007 Share Posted July 15, 2007 $num = 0.014; number_format($num, 0, "."); Quote Link to comment 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 Quote Link to comment 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... Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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 ^^ Quote Link to comment 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; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.