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? Link to comment https://forums.phpfreaks.com/topic/105424-solved-round-help/ 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 Link to comment https://forums.phpfreaks.com/topic/105424-solved-round-help/#findComment-539893 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. Link to comment https://forums.phpfreaks.com/topic/105424-solved-round-help/#findComment-539904 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. Link to comment https://forums.phpfreaks.com/topic/105424-solved-round-help/#findComment-539913 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) ? Link to comment https://forums.phpfreaks.com/topic/105424-solved-round-help/#findComment-539963 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. Link to comment https://forums.phpfreaks.com/topic/105424-solved-round-help/#findComment-539970 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. Link to comment https://forums.phpfreaks.com/topic/105424-solved-round-help/#findComment-539971 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? Link to comment https://forums.phpfreaks.com/topic/105424-solved-round-help/#findComment-540037 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"; ?> Link to comment https://forums.phpfreaks.com/topic/105424-solved-round-help/#findComment-540055 Share on other sites More sharing options...
phpretard Posted May 13, 2008 Author Share Posted May 13, 2008 Thank you! Link to comment https://forums.phpfreaks.com/topic/105424-solved-round-help/#findComment-540057 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. Link to comment https://forums.phpfreaks.com/topic/105424-solved-round-help/#findComment-540067 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> Link to comment https://forums.phpfreaks.com/topic/105424-solved-round-help/#findComment-540135 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); ?> Link to comment https://forums.phpfreaks.com/topic/105424-solved-round-help/#findComment-540168 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.