php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68375 function floatval
Submitted: 2014-11-07 13:27 UTC Modified: 2014-11-07 16:35 UTC
From: brulik at kdv dot cz Assigned:
Status: Not a bug Package: Variables related
PHP Version: Irrelevant OS: linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: brulik at kdv dot cz
New email:
PHP Version: OS:

 

 [2014-11-07 13:27 UTC] brulik at kdv dot cz
Description:
------------
Hello,
I found a problem when using function floatval. Problem occurs in five digit numbers. Processed data is variously evaluates.

Test script:
---------------
$number='0.00001';
$number2='1.00001';

echo floatVal($number);
echo "</ br>";
echo floatVal($number2);

Expected result:
----------------
1.0E-5
1.00001

Why is the first value of 1.0E-5 instead of values 0.00001 ?


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-11-07 16:35 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2014-11-07 16:35 UTC] nikic@php.net
The default float -> string cast uses scientific notation if the resulting output is shorter than printing the number directly (%g printf modifier). If you want to control the exact output, either use number_format or sprintf.
 [2016-01-28 12:55 UTC] spam2 at rhsoft dot net
that is nonsense-behavior, there is no sane reason for defaulting to scientific notation and at least strval() should convert it back

"number_format" generates a fixed count of decimals

strval(doubleval('0.00001')) results in "1.0E-5"


have fun sanitize vars for generate SpamAssassin rules with PHP

"FREEMAIL_SUBJECT 1.0E-5" is not valid for "score", skipping: score FREEMAIL_SUBJECT 1.0E-5
 [2016-01-28 13:13 UTC] spam2 at rhsoft dot net
oh yeah use sprintf

sprintf("%g", $out);
1.0e-5

sprintf("%01.5", '0.000001');
0

sprintf("%01.10", '0.00001');
0
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 27 18:01:30 2024 UTC