Newb here.
I'm trying to build a form which will take in two amounts then return a set of simple calculations based on data entered in by the user (the amount of the sub tax, the amount of the sub total, the amount of the total, etc.
I attempted to build a function and bolt it in round the amounts to the nearest penny, but it's not working as hoped. It seems like its' rounding up the nearest 100 dollar mark i think. I've googled a bit, read through stack some, and i'm becoming more and more confused as i attempt what i thought would be a simple exercise.
Guidance help appreciated
my code:
<?php //0102e_taxes-01.php
//Constant never changes, 100% available everywhere- almost always simple datatypes (T or F, num, string)
//define('THIS_PAGE', $_SERVER['PHP_SELF']);//shows webroot
define('THIS_PAGE', basename($_SERVER['PHP_SELF']));//shows app root - gets name of the file basically
define('TAX_RATE', .095); //current tax rate us => .095 => is 9.5%
//echo THIS_PAGE;// shows this pagefile
//VAR_DUMP - we love it cause we can see everything
function roundNearestHundredUp($number) {
return ceil( $number / 100 ) * 100;
}
if (isset($_POST['submit'])) {//if data, do processing
$purchase01 = (float) $_POST['purchase01'];//float before floats data after
$purchase02 = (float) $_POST['purchase02'];//float before floats data after
$subtotal = $purchase01 + $purchase02;
$amountTotal = TAX_RATE * $amountBeforeTax;
$amountTax = $amountTotal - $subtotal;
echo "<br /><br />";
echo 'First Purchase: $' . $purchase01 . '<br />';//standard
echo 'First Purchase: $' . $purchase02 . '<br />';//standard
echo '<hr />';
echo 'subtotal before tax: $' . roundNearestHundredUp($subtotal) . '<br />';//standard
echo '<hr />';
echo 'Washington state tax: %' . TAX_RATE . '<br />';
echo '<hr />';
echo 'your purchase amount is $' . roundNearestHundredUp($amountTotal) . '<br />';//risky double dollar sign
}else{//show Output
echo
'
<form method="post" action="004_form-taxes.php">
Enter Purchase Amount:
<input type="text" name="purchase01" />
<br />
Enter Purchase Amount:
<input type="text" name="purchase02" />
<br />
<input type="submit" name="submit" />
</form>
';
}//if you dont' need else, do a 'print and die statement' so if else happens you know else happened.
?>
ceil()
before you found round()?? You will also greatly benefit from the BC Math Functions for mathematical calculations due to 32-bit int/float limitations and PHP's loose type acceptance in general.echo '$'.round(160.1662, 2);
will produce$160.17