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

 

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.