Jump to content


Photo

Count numbers after decimal point


  • Please log in to reply
5 replies to this topic

#1 Tjk

Tjk
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 31 July 2006 - 09:53 AM

Is there a way of counting the number of numbers after the decimal point of a sum?

For example...

<?
$sum= 100/$a_number;

//if $sum has so many numbers after a decimal point then do something else do something different
?>

Help appreciated as always. :)


#2 BillyBoB

BillyBoB
  • Members
  • PipPipPip
  • Advanced Member
  • 630 posts

Posted 31 July 2006 - 10:03 AM

are you trying to make the numbers behind the decimal go away like if the number was 1.1111111111 and u want 1.11 thats called the float might look that up
i havnt touched that in php before if thats not wat u need please inform me more :)

#3 Tjk

Tjk
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 31 July 2006 - 04:33 PM

:) No it isn't.

Basically if the $sum has the value 2.333333444445555 then the script will do something like add so much to another variable. But if the value is only 2 then it will do something like takeaway so much to this other variable.

So far I've been unable to find a way of doing it.

Thanks for any help you can give me.

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 31 July 2006 - 04:52 PM

You can use the explode() function to get that. Here's a short example:
<?php
$x = 3574/297;
echo $x.'<br>';
$y = explode('.',$x);
echo '<pre>' . print_r($y,true) . '</pre>';
echo 'The number of digits after the decimal point is: ' . strlen($y[1]);
?>

Ken

#5 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 31 July 2006 - 04:54 PM

How would you distinguish between 2, 2.0, and 2.0000000000000000000, all of which have different numbers after the 2.

Perhaps you want to distinguish between a number that is an integer and one that isn't.

The ".. has so many numbers after a decimal point then do something else do something different" is just too vague.

Maybe Ken's solution is what you want. If not, explain clearly.
Legend has it that reading the manual never killed anyone.
My site

#6 Vinze

Vinze
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 31 July 2006 - 05:12 PM

Ken's solution could work, so could this:

<?php
// Set $number to the number you want to check
$length = strlen(stristr(strval($number), '.'));
?>

WTH? I became a guru by asking questions!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users