Jump to content

Archived

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

Tjk

Count numbers after decimal point

Recommended Posts

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

For example...

[code=php:0]
<?
$sum= 100/$a_number;

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

Help appreciated as always. :)

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
:) 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.

Share this post


Link to post
Share on other sites
You can use the explode() function to get that. Here's a short example:
[code]<?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]);
?>[/code]

Ken

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Ken's solution could work, so could this:

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

Share this post


Link to post
Share on other sites

×

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.