note 103755 added to function.sprintf
| From: | timo dot frenay at gmail dot com | Date: | Mon, 02 May 2011 12:38:37 +0000 |
| Subject: | note 103755 added to function.sprintf | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-179336@lists.php.net to get a copy of this message | ||
Here is how to print a floating point number with 16 significant digits regardless of magnitude:
<?php
$result = sprintf(sprintf('%%.%dF', max(15 - floor(log10($value)), 0)), $value);
?>
This works more reliably than doing something like sprintf('%.15F', $value) as the latter
may cut off significant digits for very small numbers, or prints bogus digits (meaning extra digits
beyond what can reliably be represented in a floating point number) for very large numbers.
----
Server IP: 69.147.83.197
Probable Submitter: 83.160.79.214
----
Manual Page -- http://www.php.net/manual/en/function.sprintf.php
Edit -- https://master.php.net/note/edit/103755
Del: integrated -- https://master.php.net/note/delete/103755/integrated
Del: useless -- https://master.php.net/note/delete/103755/useless
Del: bad code -- https://master.php.net/note/delete/103755/bad+code
Del: spam -- https://master.php.net/note/delete/103755/spam
Del: non-english -- https://master.php.net/note/delete/103755/non-english
Del: in docs -- https://master.php.net/note/delete/103755/in+docs
Del: other reasons-- https://master.php.net/note/delete/103755
Reject -- https://master.php.net/note/reject/103755
Search -- https://master.php.net/manage/user-notes.php