If you have two numbers and you divide them, how do you limit how many numbers go past the decimal. For example, if you devide 80 by 17 how do you get it to say 4.70 not 4.70588235294117647058823529411765. Thanks.
There is no built-in method for adding decimal places "only when necessary", but using some regular expressions you can easily strip off unneeded ones afterwards. Here's a file that demonstrates the code:
<!-- numtest.php -->
<HTML>
<HEAD></HEAD>
<BODY>
<?php
if ($submit) { $n=number_format($n,2);
$n = ereg_replace("0*$","",$n);
$n = ereg_replace("\.$","",$n);
echo("<P>Result = $n");
}
?>
<FORM ACTION=<?=$PHP_SELF?> METHOD=POST>
<P>Type a number to format:<INPUT TYPE=TEXT NAME=n VALUE=<?=$n?>>
<INPUT TYPE=SUBMIT NAME=submit>
</FORM>
</BODY>
</HTML>
Bookmarks