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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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: Wed May 08 14:01:34 2024 UTC