Re: counting the digits in a number, exponential format problem [message #176341 is a reply to message #171001] |
Fri, 23 December 2011 19:00 |
gordonb.hojoy
Messages: 1 Registered: December 2011
Karma:
|
Junior Member |
|
|
> @Jerry: i totally agree with you that PHP must convert a numeric value
> passed to strlen() to a string at some point. what i do not understand
> though is why it makes a difference whether the numeric is 1E-4 or
> 1E-5. it is like that single order of magnitude drastically changes
> the logic/conversion process of the numeric to a string and causes the
> answer to not, as you say, 'be what i want'.
Read the description of the (C language, but also applies to PHP)
*printf format conversion %g. "The argument is printed in style f
or in style e, whichever gives full precision in minimum space".
Guess where it switches?
I assume that in PHP, strlen($x), where $x is a floating-point
number, is equivalent to strlen(sprintf("%g", $x)), and experimentation
seems to bear this out.
I suggest that you explicitly convert the number into a string using
the format that you want (e.g. %f, not %g, or else use a format
explicitly specifying precision) and apply strlen() to that. Do
not apply strlen() to floating-point numbers.
> it is just not consistent at all. imho a function like strlen() should
> pick one way of handling a type of input and do it consistently, not
> vary its approach depending on the size of a value passed to it...
> especially when it is a weakly typed language.
I suspect that strlen() does not get to pick anything. It's done before
strlen() gets its hands on it.
> it appears as though PHP might use something similar to the Linux
> function strtod(3) to do its string to float conversions. however, the
You are concerned with float to string conversions, not the other
way around.
> man page:
> http://linux.die.net/man/3/strtod
> does not shed any light on my problem.
|
|
|