Stop PHP 5 from Rounding

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hey everyone!  I'm a complete newbie to PHP, and am trying to teach
myself how to make some scripts.  So far I've done pretty good, but
even after searching through all my books, articles, manuals, and
search engines on PHP, I can't figure out how to stop PHP from
rounding.  I am trying to create a calculator that calculates a
diameter * pi, and I got it, but I can't stop it from rounding to a few
digits, which is very annoying.  I'm looking at calculating like ten
thousand digits and printing all of them (I know this doesn't sound
convenient, but it will not be used for public use, just for some
clients of mine that say they want accuracy).  So far my calculator
looks like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
" ">
<html xmlns=" ">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
<title>Pi Calculator</title>
<form action="" method="post">
<input name="diameter" type="text" size="10" maxlength="500" />
<select name="method1">
    <option value="add" selected="selected">+</option>
    <option value="sub">-</option>
    <option value="multi">*</option>
    <option value="div">/</option>
<input name="submit1" type="submit" value="Submit" />
<input name="reset1" type="reset" value="Reset" />
// Get variables from POST:
$submit1 = $_POST['submit1'];
$submit2 = $_POST['submit2'];
$method1 = $_POST['method1'];
$method2 = $_POST['method2'];
$diameter = $_POST['diameter'];
$second = $_POST['second'];
$number = $_POST['number'];

// Simple
if ($submit1 == true) {
    switch ($method1) {
        case add: $ans = $first + $second;
        case sub: $ans = $first - $second;
        case multi: $ans = $diameter * $pi;
        case div: $ans = $first / $second;

$ans = number_format($ans, 500, '.', ',');
echo "<p>$ans</p>";

I know it isn't finished yet and that there is a lot of unneeded code,
but I will fix that once I stop it from rounding.  variables.php just
has pi in it.  If you can help me out I'd really appreciate it!

Re: Stop PHP 5 from Rounding

I dont see anything wrong with it... actually when I try your script on
my editor I get a really big number like 100 or so digits.

Did you try to do the same without the line $ans = number_format($ans,
500, '.', ','); ?

Re: Stop PHP 5 from Rounding

Jackson Peebles wrote:
Quoted text here. Click to load it

Hi there

Floating point numbers are usually only accurate to about 14 decimal
places and only close approximations of the number you expected.

Quoted text here. Click to load it

Check out the bcmath library, it is accurate to any number of digits
you need but it comes at the expense of speed.

The user comments say the last few digits are approximate so if you
need accuracy to a 100 digits use 104 digits and ignore the last 4...


Re: Stop PHP 5 from Rounding

Quoted text here. Click to load it
The returned float has a precision based on the precision directive in
php.ini, which defaults to 14.

Checkout your php.ini configuration :

; The number of significant digits displayed in floating point numbers.
precision    =  14

To configure on the fly use this function :


ini_set('precision', '14');




Re: Stop PHP 5 from Rounding

lorento wrote:
Quoted text here. Click to load it

The precision setting alters the number of digits displayed in echo
statements but it does not affect the precision of floating points

Quoted text here. Click to load it
The size of a float is platform-dependent, although a maximum of
~1.8e308 with a precision of roughly 14 decimal digits is a common
value (that's 64 bit IEEE format).

For higher accuracy a software math library is needed. Bcmath does
this, slowly...



Site Timeline