Jump to content

[SOLVED] Moving the decimal two places over.


pocobueno1388

Recommended Posts

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 :)

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.