This is a discussion on Floats and avoid exponential notation - How? within the PHP General forums, part of the PHP Programming Forums category; Hi, From http://www.php.net/manual/en/language.types.float.php (second commentin that page, from "kjohnson at ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi, From http://www.php.net/manual/en/language.types.float.php (second commentin 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 operationwith 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 |
|
|||
|
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 |
|
|||
|
"k bah" <kbah@linuxmail.org> 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 :) |
|
|||
|
On Jul 17, 7:23*am, k...@linuxmail.org ("k bah") wrote:
> 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. http://php.net/manual/en/function.bcadd.php Or without BCMath write an equivalent for integers. Steve |