Simple Javascript Help!

Discussion in 'OT Technology' started by mondaynightmike, Jan 12, 2005.

  1. mondaynightmike

    mondaynightmike New Member

    Joined:
    Aug 10, 2004
    Messages:
    117,672
    Likes Received:
    0
    Location:
    DEFINITELY No *Unlinked* Cock Pics
    Hello all i've got a some in javascript which add's all these values:

    Code:
    vatprice = (document.mf.document.mf.cpuprice.value*1 + document.mf.mbprice.value*1 + document.mf.ramprice.value*1
      + document.mf.caseprice.value*1 + document.mf.hdprice.value*1 + document.mf.graphicsprice.value*1
      + document.mf.monitorprice.value*1 + document.mf.printerprice.value*1 + document.mf.mouseprice.value*1
      + document.mf.keyboardprice.value*1 + document.mf.storageprice.value*1 + document.mf.cddvdprice.value*1
      + document.mf.cddvd2price.value*1 + document.mf.networkprice.value*1 + document.mf.modemprice.value*1
     + document.mf.scannerprice.value*1 + document.mf.speakersprice.value*1 + document.mf.webcamprice.value*1)
     
    then displays them using this code:

    Code:
     document.mf.vatprice.value = vatprice;
     
    does anyone know what code i need to add in, in order to add 17.5% to the value but at the same time keeping it rounded up.

    I've tried and i've ended up with numbers like 34.324234234 which is correct but i only want the first 2 after the decimal.

    can anyone help?
     
  2. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
    This is what I used in VB. I'm too lazy to understand it again but it does work:

    Code:
    Function ProperRound(number As Double, Optional places As Integer = 0) As Double
    
         ProperRound = Sgn(number) * Int((Abs(number) + ((10 ^ -places) / 2)) * 10 ^ places) / 10 ^ places
    
    End Function
    Edit: 0 places doesn't quite work all the time but it will work for 2

    Edit2: Fuck - I dunno this is some old code that I didn't finish - but you get the idea - needs to be bug fixed

    Edit3: Alright fixed. I think.

    Edit4: So, you didn't really see these changes and now I realize why I changed it (but that didn't work). If you put in 39.995 and round it to 2 places, it outputs 39.99 instead of 40. It does this because it takes 39.995 adds .005 to it to get 40.000. I then multiply it by 100 to get 4000.0. It then Integers that number, but instead of properly integering it to 4000, it integers it to 3999. I don't know why. I then divide it by 100 to get it back to the proper places.

    In other words, I started fucking around with the code after I realized there was a problem, created a new problem and all of this was because the code that was correct, did not produce the proper results.
     
    Last edited: Jan 12, 2005

Share This Page