Jump to content

Count numbers after decimal point


Tjk

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. :)
Link to comment
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.
Link to comment
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
Link to comment
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.
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.