Re: counting the digits in a number, exponential format problem [message #170998 is a reply to message #170993] |
Tue, 14 December 2010 15:27 |
mhandersen
Messages: 5 Registered: December 2010
Karma:
|
Junior Member |
|
|
maybe i did not word my question well enough... let me try again.
first of all, this is not a question about machine precision. i am
well aware of the limitations of floating point values and that some
(many) numbers are not actually able to be exactly represented by a
computer. for the sake of argument lets forget that, because i do not
think my question relates to it at all.
at its core, my question really wants to know why there is an
inconsistency in PHP shown by the following example:
strlen(3E-1) = strlen(0.3) = 3
strlen(3E-2) = strlen(0.03) = 4
strlen(3E-3) = strlen(0.003) = 5
strlen(3E-4) = strlen(0.0003) = 6
but then for some reason
strlen(3E-5) = strlen((float)3E-5) = strlen(0.00003) = 6
strlen(3E-6) = strlen((float)3E-5) = strlen(0.000003) = 6
don't get hung up on the numbers. this has happened for any number i
have chosen in the jump between E-4 and E-5. there just seems to be
some inconsistency in how PHP internally represents the variables. and
i would like to know if anyone know what it is, why it is, and/or how
to avoid it?
also, in response to Jerry, PHP is a weakly typed language which means
it doesn't require (nor support for that matter) explicit type
declaration of variables. so saying that strlen only works on strings
doesn't make sense. in PHP a variable is a variable is a variable.
there is no difference.
in response to Alvaro, the reason i would like to count the digits is
because i do not know how wide each number is in a set of numbers and
for each number i need to set a width for a placeholder for the
number. i do not want to force a certain number of digits to always be
outputted so it is constant width, so i would like to adjust my
placeholder width based on the number width.
|
|
|