Jump to content

[SOLVED] round (HELP)


phpretard

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.