1

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.
?>
5
  • I'm just saying that a payment processing application is a very complicated system to design correctly. Use something off the shelf and follow the instructions to the tee. Even better, hire someone with lots of knowledge and experience for this part of your site.
    – krowe
    Commented Apr 11, 2014 at 16:51
  • You found 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.
    – MonkeyZeus
    Commented Apr 11, 2014 at 16:52
  • I'm just trying to figure out how to round something to the nearest penny as an exercise - i'm not attempting to build something that is going to go into product. Taking something off the shelf really won't teach me anything other then that i can take something off the shelf which i already know: Doing something like that when trying to learn to cook is like using a TV dinner and calling it cooking (make sense?) I'm trying to LEARN.
    – Chezshire
    Commented Apr 11, 2014 at 16:54
  • echo '$'.round(160.1662, 2); will produce $160.17
    – MonkeyZeus
    Commented Apr 11, 2014 at 16:55
  • Thank you MonkeyZeus, it took me a bit to understand what you said, but now that I do this was exactly what i was looking for; I'm just trying to learn - not build a real application, just learn so that maybe one day i can or at least kept the debs i work with from cringing when I send them stuff
    – Chezshire
    Commented Apr 12, 2014 at 0:13

4 Answers 4

5

Look up these three functions for reference:

  1. round() - https://www.php.net/manual/en/function.round.php
  2. ceil() - https://www.php.net/manual/en/function.ceil.php
  3. number_format() - https://www.php.net/manual/en/function.number-format.php

If you only want to round up, ceil is a bit difficult to use, you will have to hack it

$amount = 14.754

round will make it 14.75 ceil will make it 15, and you lose the pennies, but you call it like this - easy to use hack

echo ceil($amount*100)/100;

+++ later edit +++

For a nicer way to do things, you can play with it a little:

echo round(ceil(amount*1000)/1000,2);
4
  • Thank you for the suggestions and I will look at them
    – Chezshire
    Commented Apr 12, 2014 at 12:19
  • I'm not sure if you edited your response or added too it but i received an update notice regarding it and will look at everything here again tonight - thanks!
    – Chezshire
    Commented Apr 15, 2014 at 19:04
  • I marked the place where i edited above with "+++ later edit +++" you can't miss it
    – ied3vil
    Commented Apr 15, 2014 at 20:16
  • Thank you - i'm really looking forwards to digging into this again tonight as it was a real expander for my understanding on several points.
    – Chezshire
    Commented Apr 15, 2014 at 22:14
2

The ceil() function means "ceiling". Therefore it will always give the closest int > $number.

I suggest you to use round() instead.

1
  1. You don't need to explicitly typecast (float)
  2. You may directly use ceil($number) or round($number)
1

First of all, dividing by 100 and rounding/ceiling up to the next integer will round to 100's of dollars, not cents! The original price is $137.8234. Divide by 100: 1.378234. Round or ceiling: 1 or 2. Multiply by 100: $100.00 or $200.00. You want to multiply by 100 first to get cents: $137.8234 => 13783.4. Round or ceiling: 13783 or 13784. Divide by 100: $137.83 or $137.84.

How to round is a whole study subject in and of itself. The simplest way is to round nickels (5) up all the time, but that introduces a bias. You can round (5) consistently to the nearest even (or odd) number to reduce the skewing of results. In any case, you usually don't want to use ceiling(), as that introduces a substantial bias.

1
  • Thank you lots for your insights Phil. I understand what the function i wrote did, but i didn't understand why and now I do so i learned a bit today which is super cool. When i got up this morning and had all my homework for the week done already, didn't realize that the little something i came up with for myself was such a big thing - it's super amazing how stuff works and works out sometimes. Super educational day for me, thanks!
    – Chezshire
    Commented Apr 12, 2014 at 0:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.