473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Stop PHP 5 from Rounding

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"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
/>
<title>Pi Calculator</title>
</head>
<?php
include("variables.php");
?>
<body>
<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>
</select>
<input name="submit1" type="submit" value="Submit" />
<input name="reset1" type="reset" value="Reset" />
</form>
<?php
// 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;
break;
case sub: $ans = $first - $second;
break;
case multi: $ans = $diameter * $pi;
break;
case div: $ans = $first / $second;
break;
}

$ans = number_format($ans, 500, '.', ',');
echo "<p>$ans</p>";
}
?>
</body>
</html>
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!

May 28 '06 #1
4 3846
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, '.', ','); ?

May 28 '06 #2
tim

Jackson Peebles wrote:
Hey everyone!
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.
Hi there

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

http://uk.php.net/manual/en/language.types.float.php
http://docs.sun.com/source/806-3568/ncg_goldberg.html
I'm looking at calculating like ten thousand digits and printing all of them


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...

http://uk.php.net/manual/en/ref.bc.php

Tim

May 29 '06 #3
>From the php manual :
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 :

<?php

ini_set('precision', '14');

?>
Regards,

Lorento
--
http://www.mastervb.net
http://www.immersivelounge.com

May 29 '06 #4
tim

lorento wrote:
From the php manual : The returned float has a precision based on the precision directive in
php.ini, which defaults to 14.


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

http://www.php.net/manual/en/language.types.float.php
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...

Regards

Tim

May 29 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: engsol | last post by:
I'm using Python to parse a bunch of s/w test files and make csv files for later report generation by MS ACCESS....(my boss loves the quick turn-around compared to C). Each log file may contain one...
4
by: spebola | last post by:
I am using vb.net 2003 professional and I get the following results when using the round method: dim Amount as decimal = 180.255 Amount = Amount.Round(Amount, 2) Amount now contains 180.25. ...
8
by: Zorpiedoman | last post by:
Howcome: Dim D as decimal = .5D msgbox d.Round(D, 0) this returns "0" Now when I went to school .5 rounds UP to 1 not DOWN to zero?????!!! Documentation says this, but what the heck are...
2
by: Jiri Nemec | last post by:
Hello all, I have got one table with rounding values, table contains prices and round types. id price_from price_to rounding 1 0 1500 0.1 2 1500 ...
11
by: cj | last post by:
Lets assume all calculations are done with decimal data types so things are as precise as possible. When it comes to the final rounding to cut a check to pay dividends for example in VB rounding...
4
by: mantrid | last post by:
I am trying to take an integer and divide it by 100 to give a decimal to store in my database. The field in my mysql database is set to decimal(4,2) My php is $price=number_format($price/100,2);...
18
by: jdrott1 | last post by:
i'm trying to round my currency string to end in 9. it's for a pricing application. this is the function i'm using to get the item in currency: FormatCurrency(BoxCost, , , , TriState.True) if...
206
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
20
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.