simple math script divide and then round up 1 integer

Discussion in 'JavaScript' started by jungledsales, Aug 13, 2008.

  1. #1
    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???
     
    jungledsales, Aug 13, 2008 IP
  2. yleiko

    yleiko Peon

    Messages:
    74
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    x = the number user sends
    y = max number a package can have
    z = number of packages

    z = parseInt( (x - 1 ) / y ) + 1
     
    yleiko, Aug 14, 2008 IP
  3. xlcho

    xlcho Guest

    Messages:
    532
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    A better way to do the same:
    z = Math.ceil(x/y);
    Code (markup):
     
    xlcho, Aug 14, 2008 IP