Re: Floats and avoid exponential notation - How?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



"k bah" <kbah@xxxxxxxxxxxxx> wrote on 07/17/2008 05:23:40 AM:

>  Hi,
> 
>  From http://www.php.net/manual/en/language.types.float.php (second 
comment in
> that page, from "kjohnson at zootweb dot com"):
> 
> "PHP switches from the standard decimal notation to exponential notation 
for 
> certain "special" floats.
> 
>  I have the same problem. I have a big number I have to represent, it's 
> usually "1" followed by 10 "zeros", the biggest value I'll 
> have for it is 19999999999, never more than this. I only make one 
operation 
> with it, (+), most of the time I need that number as a 
> string, and never need it's float representation, only the absolute 
value (in 
> fact, it's never going to have a fractional part). I 
> cannot use integers because it's bigger than the integer range. 
> 
>  If it goes to it's exponential representation, breaks my code. Users 
are 
> identified by that number.
> 
>  So, any suggestions/thoughts?
>  Is there a way to prevent php from using the exponential notation for a 
float?

I don't know of any way to modify PHP's behavior with regard to this. 
That's not to say there isn't a way, but I don't know of one.

This might be a brute force way to address it. You'll want to add 
additional checks so you don't end up in any infinite loops. You should 
definitely test with larger numbers in the range that you are actuallly 
using. Hope this gets you what you need:

// example: one of the "special" ones, prints in exponential notation, 
"1.4E+6"
$num = 1400000.;

if(strstr($num, 'E')) {
  echo "yep, exp notation<br>";
  list($significand, $exp) = explode('E', $num);
  list($void, $decimal) = explode('.', "$significand");
  $decimal_len = strlen("$decimal");
  $exp = str_replace('+', '', "$exp");
  $exp -= $decimal_len;
  $append = '';
  for($i = 1; $i <= $exp; $i++) {
    $append .= '0';
  }
  $tmp = str_replace('.', '', "$significand");
  $reconsctructed = "$tmp" . "$append";
  echo '<pre>reconstructed: ', "$reconsctructed", '</pre>';
}

kjohnson at zootweb dot com :)

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux