Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    simple math script divide and then round up 1 integer

    I need a simple javascript that will take a user-inputted number and then divide it by 900 and round up to the next integer.

    For example, a customer wants 2000 units and we sell units in packages of 900. So the customer would type in 2000 in a box and press "submit" and a result would be displayed notifying them to purchase 3 boxes. (2000/900 = 2.22 and since we don't break up boxes, they would need to purchase 3 full boxes)

    (Exception to rounding up... Obviously if the customer wanted a perfect factor of 900, I wouldn't need to round up an integer... if the customer ordered 1800 they would need 2 boxes)

    Help???

  • #2
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    Code:
    function cases(amount){
        cases = Math.round(amount/900);
    }
    That will do the math if you impliment it like this
    Code:
    <input type="text" onchange="cases(this.value);" />

  • #3
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    If you want to round up every time, you should use Math.ceil instead of Math.round.

    dumpfi
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •