Re: counting the digits in a number, exponential format problem [message #171003 is a reply to message #171001] |
Tue, 14 December 2010 19:51 |
ED
Messages: 1 Registered: December 2010
Karma:
|
Junior Member |
|
|
"-matt" <mhandersen(at)gmail(dot)com> wrote in message
news:2592bddb-447c-4fd3-afae-da8bb35cfa0c(at)r29g2000yqj(dot)googlegroups(dot)com...
> certainly at some level PHP must know the difference between a string
> and a float, but you can't a define a variable yourself as such. all i
> was saying is that strlen() takes a variable, type of that variable
> cannot be declared. i understand that strlen is intended for use with
> 'strings' but it also works for floats up to E-4 after which it breaks
> down... all i am wondering is why???
>
> i tried the strlen(sprintf("0.03%f",$mynum)) suggestion (and several
> iterations of it) but no luck. i am not sure where PHP makes the
> decision on whether or not to convert to a decimal but that doesn't
> appear to help.
>
> @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'.
>
> 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.
>
> it appears as though PHP might use something similar to the Linux
> function strtod(3) to do its string to float conversions. however, the
> man page:
> http://linux.die.net/man/3/strtod
> does not shed any light on my problem.
hi Matt,
The strval function should shed some light here in seeing how php interally
represets the string value of the floats, eg:
echo strval(3E-1); // '0.3'
echo strval(3E-2); // '0.03'
echo strval(3E-3); // '0.003'
echo strval(3E-4); // '0.0003'
echo strval(3E-5); // '3.0E-5'
echo strval(3E-6); // '3.0E-6'
ie after 4 decimal places php will represent the string value of the float
in exponential format.
also see -
http://www.php.net/manual/en/language.types.string.php#language.types.strin g.casting
"An integer or float is converted to a string representing the number
textually (including the exponent part for floats). Floating point numbers
can be converted using exponential notation (4.1E+6)."
Cheers
ED
|
|
|