FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » counting the digits in a number, exponential format problem
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
counting the digits in a number, exponential format problem [message #170979] Mon, 13 December 2010 18:46 Go to previous message
mhandersen is currently offline  mhandersen
Messages: 5
Registered: December 2010
Karma:
Junior Member
Hi,

I was wondering if anyone had a good way of counting the digits in a
given (general) number.

Obviously this is trivial for certain numbers. For example, the number
of digits in 523 (or any other natural number) can be found either by:

ceil(log10(523)) or strlen(523)… and I am sure there are other ways.

Once you start dealing with floats it gets a little more complicated,
but there are solutions. For example, the number of digits in 3.3 can
be found with:

$value = 3.3;
if ((int)$value == $value)
{
return strlen($value);
} else {
return strlen($value)-1; //the -1 removes the count for the ‘.’
}

In general the above equation SHOULD work for any float or double, but
it does not. My problem arises when PHP uses exponential formatting
and for some reason there is a break down at E-5. For example:

$value = 3.3E-4;
if ((int)$value == $value)
{
return strlen($value);
} else {
return strlen($value)-1;
}

returns the value 6, which is correct. (There are 6 digits in
0.00033). However, if we try:

$value = 3.3E-5;
if ((int)$value == $value)
{
return strlen($value);
} else {
return strlen($value)-1;
}

it returns 5. The reason it does this is because at E-5 it appears
that PHP stops using the full decimal value and leaves it in the
exponential format. Thus strlen no longer makes sense because it does
strlen(3.3E-5) (and treats not only the ‘.’ but also the ‘E’ and ‘-‘
as characters and includes them in the length calculation) instead of
converting the number to a value and performing strlen(0.000033). Even
if I alter the function to cast the value [return strlen((float)
$value)-1;] it still returns 5.

There is some difference in PHP between numbers which are E-4 and
numbers which are E-5. The numbers I care about go out to E-8.

If anyone knows why PHP does this, how to force PHP to not use
exponential formatting, or how to count digits in an exponentially
formatted float less than E-4 I would appreciate the help.

P.S. I have tried ini_set("precision", "12"); and the like, but that
does not help. My interest is not in adjusting PHP’s precision, only
how it treats the numbers.
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: width measurements?
Next Topic: cheap, discount nike air max shoes, paypal payment, free shipping
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Tue Nov 26 17:38:09 GMT 2024

Total time taken to generate the page: 0.04644 seconds