phpretard Posted May 13, 2008 Share Posted May 13, 2008 Howdy, I have a little problem... $grandtotal="195.20264"; round($grandtotal , 2); This only displays: 195.2 My problem is I need the "0" to show up as well : 192.20 How can I fix this? Anybody know? Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 13, 2008 Share Posted May 13, 2008 I've never had any problems with number_format(). http://us.php.net/manual/en/function.number-format.php Quote Link to comment Share on other sites More sharing options...
phpretard Posted May 13, 2008 Author Share Posted May 13, 2008 The problem is with the Zero not showing up. The round() works fine. If the second number defined by the 2 in round($var, 2) is = to Zero it will not display the Zero. Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 13, 2008 Share Posted May 13, 2008 And what I am saying is that, round may work well, but number_format will show the ZERO. Did you even trying using number_format? It's the same parameters and also the same output, EXCEPT I actually see the zeros. Quote Link to comment Share on other sites More sharing options...
phpretard Posted May 13, 2008 Author Share Posted May 13, 2008 do you mean number_format($var, 2) as opposed to round($var, 2) ? Quote Link to comment Share on other sites More sharing options...
AndyB Posted May 13, 2008 Share Posted May 13, 2008 do you mean ... I expect he does and/or expects you to look in the manual where all is explained. That's why he posted a link for you. Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 13, 2008 Share Posted May 13, 2008 I'm not sure if number_format does rounding. IE if you have a number like 2.555, it will give you 2.55 instead of 2.56 But using both of them at the same time should give you the desired result. Quote Link to comment Share on other sites More sharing options...
phpretard Posted May 13, 2008 Author Share Posted May 13, 2008 I have read the manual and none of the examples or numbers usa a Zero as a one hundreth. $Example="1.50549976546" round($Example, 2) returns 1.5 (notice the Zero missing) I need the zero to show up. Can you help? Quote Link to comment Share on other sites More sharing options...
benphp Posted May 13, 2008 Share Posted May 13, 2008 <?php $example = round(195.20264, 2); print "$example<P>"; $ex = explode(".", $example); if (strlen($ex[1]) == 0) { $example = $example."00"; } else if (strlen($ex[1]) == 1) { $example = $example."0"; } print "$example"; ?> Quote Link to comment Share on other sites More sharing options...
phpretard Posted May 13, 2008 Author Share Posted May 13, 2008 Thank you! Quote Link to comment Share on other sites More sharing options...
benphp Posted May 13, 2008 Share Posted May 13, 2008 I found a bug in that script. It'll give you a start though. Quote Link to comment Share on other sites More sharing options...
benphp Posted May 13, 2008 Share Posted May 13, 2008 Here's the fixed code made into a function <html> <head> <title>Rounding</title> </head> <body> <?php $example = "6"; $exampleOrig = ""; $print = ""; if (isset($_POST['example'])) { $example = fnRoundZero($_POST['example']); $exampleOrig = $_POST['example']; $print = "Original: ".($exampleOrig)."<br />Rounded: $example <br />"; } function fnRoundZero ($example){ $example = $example; $example = round($example, 2); $ex = explode(".", $example); if (count($ex) > 1 && strlen($ex[1]) == 0) { $example = $example."00"; } else if (count($ex) > 1 && strlen($ex[1]) == 1) { $example = $example."0"; } else if (count($ex) == 1) { $example = $example.".00"; } return $example; } print "<form method=\"post\">"; print "Number: <input type=\"text\" name=\"example\" value=\"$exampleOrig\" size=\"6\" maxlength=\"6\"><br />"; print "<input type=\"submit\" name=\"btnSubmit\" value=\"Round It\">"; print "</form>"; print "$print"; ?> <br /><br /> </body> </html> Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 13, 2008 Share Posted May 13, 2008 What about <?php echo number_format(round($num, 2), 2); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.