TRI0N Posted June 28, 2007 Share Posted June 28, 2007 Okay here is a simple script that will add up a running total. My problem is that I want it to output leading zeros at the end. Example: Its putting out 24 when I want it to be 24.00 or if there is cents in this to show it as. 24.50 not 24.5. function addMe(){ v1=document.getElementById('rounds').value v2=document.getElementById('players').value v3=document.getElementById('cart').value v4=document.getElementById('room_rate').value v5=document.getElementById('no_nights').value wd_rate=24.00 we_rate=29.00 c_rate=15.00 if(document.getElementById('rounds').value == 1){ vT1=(Number(v2) * Number(wd_rate)) }else{ vT1=(Number(v2) * Number(wd_rate)) - Number(v1) } if(document.getElementById('cart').value > 0){ vT2=(Number(v2) * Number(c_rate)) }else{ vT2=0.00 } vT3=(Number(v4) * Number(v5)) vT=Number(vT1) + Number(vT2) + Number(vT3) document.getElementById('tot').innerHTML=vT ; } Thanks for any help on this! Quote Link to comment Share on other sites More sharing options...
mkoga Posted June 28, 2007 Share Posted June 28, 2007 try something like this: function pad(num) { var result = num; if (result.indexOf('.') != -1) { var tmp[] = num.split('.'); var suffix = tmp[1]; if (suffix.length < 1) { suffix = "00"; } else if (suffix.length == 1) { suffix += "0"; } else if (suffix.length > 2) { suffix = suffix.substring(0, 2); } result = tmp[0] + "." + suffix; } else { result += ".00"; } return result; } Quote Link to comment Share on other sites More sharing options...
TRI0N Posted June 28, 2007 Author Share Posted June 28, 2007 Not sure how to convert what you got there but let take vT and see how would I run the given script above a way to test that varible. I tried a few ideas but they didn't work. Perhaps if somone tossed me a bone on vT as an example before it is put back into a HTML Id value. Thanks. Quote Link to comment Share on other sites More sharing options...
mkoga Posted June 28, 2007 Share Posted June 28, 2007 You can run vT through this function and use the return value as the innerHTML. Quote Link to comment Share on other sites More sharing options...
TRI0N Posted June 29, 2007 Author Share Posted June 29, 2007 The following should work then? function pad(num) { var result = vT; if (result.indexOf('.') != -1) { var tmp[] = num.split('.'); var suffix = tmp[1]; if (suffix.length < 1) { suffix = "00"; } else if (suffix.length == 1) { suffix += "0"; } else if (suffix.length > 2) { suffix = suffix.substring(0, 2); } result = tmp[0] + "." + suffix; } else { result += ".00"; } document.getElementById('tot').innerHTML=result; } Quote Link to comment Share on other sites More sharing options...
mkoga Posted June 30, 2007 Share Posted June 30, 2007 more like: document.getElementById('tot').innerHTML = pad(vT); Quote Link to comment Share on other sites More sharing options...
emehrkay Posted June 30, 2007 Share Posted June 30, 2007 ummmm i think you guys are reinventing core js methods http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Number:toPrecision var num=5.1; alert("num.toPrecision(3) is " + num.toPrecision(3)); // alerts 5.10 Quote Link to comment Share on other sites More sharing options...
mkoga Posted July 1, 2007 Share Posted July 1, 2007 awesome! never knew about the toPrecision() method. thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.