Re: Floats and avoid exponential notation - How?

From: Date: Thu, 17 Jul 2008 13:34:48 +0000
Subject: Re: Floats and avoid exponential notation - How?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-276954@lists.php.net to get a copy of this message
On Thu, Jul 17, 2008 at 7:23 AM, k bah <kbah@linuxmail.org> wrote: > > 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. You can see a partial list of > such "special" values with this:" > > Then he goes on about it and finishes with: > > " I have to be honest: this is one of the strangest things I have seen in any language in > over 20 years of coding, and it is a > colossal pain to work around." > > 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. I wrote a small function, but > cannot be sure if it's going to work (report error when the exponential notation is used > by php), mostly because on my tests, I > can't precise when and to which of these numbers php chooses to use the exponential > notation: > > --- code > function checkFloat($float_var) { > > $ar_empty = ""; > $string_var = (string)$float_var; > > $pattern = '/[0-9]|\./'; // From zero to nine and "dots" > $match_found = preg_match_all($pattern, $string_var, $ar_empty); > > unset($ar_empty); > if ($match_found != strlen($string_var)) { > return false; > } else { > return true; > } > } > --- code > > So, any suggestions/thoughts? > Is there a way to prevent php from using the exponential notation for a float? > > > thanks > > = > > > -- > Powered by Outblaze > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > Does changing the value of 'precision' in php.ini make a difference? Mine is set to 14, and had no problem rendering that number in standard notation. I'm not sure what kind of problems you're having with this format, but I think this might fix them. (Even if you pass the number in exponential notation to a database, most databases I've worked with will recognize it and handle it correctly.) If it's just a display issue, will number_format($float_value, 0, '', '') work? Andrew

« previous php.general (#276954) next »