Simple JavaScript help

Discussion in 'Software' started by Publius, Dec 15, 2007.

  1. Publius

    Publius Sergeant

    I have a simple javascript that I am using to do basic calculations in a web based lab system. Below is an example. The calculation works fine, but the result give way too many decimal places. Is there an easy way to truncate the number of decimal places displayed in the fields? I have tried using the toFixed(x) function, but am either not using it correctly or it will not work the way I need it to.

    For this example, the user enters a value for totalVol in a text field and both the acidVol and waterVol fields are automatically populated with the calculated values. I am using onKeyUp= doIt(this.form) in each of the field because it is a form embedded within a form.

    The formatting of the code looks a little strange because the system requires iXML formatting, so the "-" characters are just line continuations.

    Any suggestions would be greatly appreciated.

    Thanks in advance.
    Code:
            script
              - function doIt(_b)
              - { if(_b.totalVol.value!="" )
              -   {
              -
              -   _b.acidVol.value=_b.totalVol.value/36.8;
              -   _b.waterVol.value=_b.totalVol.value - _b.acidVol.value;
              -
              -
              -  }
              - else {
              -        _b.acidVol.value="";
              -        _b.waterVol.value="";
              -       }  
              -
              - }
              - 
     
  2. Kodo

    Kodo SNATCHSQUATCH

    ex:
    var t=1.123456;

    var shortT=t.toFixed(3); //= 1.123
     
  3. Publius

    Publius Sergeant

    Thanks Kodo. Worked perfectly.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds