Praetorian Posted November 10, 2007 Share Posted November 10, 2007 Quick question that I'm sure has an easy answer.I'm using javascript to do the math on a form and show people their sub-total. I can't figure out how to get the price result to show an extra zero at the end. At the moment, it shows up as .5 instead of .50.Here's the script. Thanks in advance for the help. <script language="javascript" type="text/javascript"> function SetPrice(){ if (isNaN(frm.ad_duration.value)) { alert("Please Enter Numeric Value for Ad Duration."); frm.ad_duration.value=1; frm.ad_duration.focus(); // return (false); } if(frm.ad_type[0].checked){ if(frm.ad_time_type[0].checked){ javaprice=parseFloat(document.frm.ad_duration.value*2);} if(frm.ad_time_type[1].checked){ javaprice=parseFloat(document.frm.ad_duration.value*7*2);} if(frm.ad_time_type[2].checked){ javaprice=parseFloat(document.frm.ad_duration.value*30*2);} } if(frm.ad_type[1].checked){ if(frm.ad_time_type[0].checked){ javaprice=parseFloat(document.frm.ad_duration.value*1);} if(frm.ad_time_type[1].checked){ javaprice=parseFloat(document.frm.ad_duration.value*7*1);} if(frm.ad_time_type[2].checked){ javaprice=parseFloat(document.frm.ad_duration.value*30*1);} } if(frm.ad_type[2].checked){ if(frm.ad_time_type[0].checked){ javaprice=parseFloat(document.frm.ad_duration.value*.75);} if(frm.ad_time_type[1].checked){ javaprice=parseFloat(document.frm.ad_duration.value*7*.75);} if(frm.ad_time_type[2].checked){ javaprice=parseFloat(document.frm.ad_duration.value*30*.75);} } if(frm.ad_type[3].checked){ if(frm.ad_time_type[0].checked){ javaprice=parseFloat(document.frm.ad_duration.value*.25);} if(frm.ad_time_type[1].checked){ javaprice=parseFloat(document.frm.ad_duration.value*7*.25);} if(frm.ad_time_type[2].checked){ javaprice=parseFloat(document.frm.ad_duration.value*30*.25);} } if(frm.ad_type[4].checked){ if(frm.ad_time_type[0].checked){ javaprice=parseFloat(document.frm.ad_duration.value*.50);} if(frm.ad_time_type[1].checked){ javaprice=parseFloat(document.frm.ad_duration.value*7*.50);} if(frm.ad_time_type[2].checked){ javaprice=parseFloat(document.frm.ad_duration.value*30*.50);} } if(frm.ad_type[5].checked){ if(frm.ad_time_type[0].checked){ javaprice=parseFloat(document.frm.ad_duration.value*.50);} if(frm.ad_time_type[1].checked){ javaprice=parseFloat(document.frm.ad_duration.value*7*.50);} if(frm.ad_time_type[2].checked){ javaprice=parseFloat(document.frm.ad_duration.value*30*.50);} } document.frm.total_price.value=javaprice;document.frm.total_price1.value=javaprice;} adtype(); SetPrice();</script> Link to comment Share on other sites More sharing options...
Reg Edit Posted November 10, 2007 Share Posted November 10, 2007 [...] I can't figure out how to get the price result to show an extra zero at the end. At the moment, it shows up as .5 instead of .50 [...]I don't think javascript has any built-in number-formatting functions--you just use the weakly-typed nature of the language and perform string operations on the variable:var i = 0.5;document.write(i + "0"); If your number could be, say, 0.55, then after appending the zero, you can use indexof() to locate the decimal point and substr() to truncate to 2 decimal places. Link to comment Share on other sites More sharing options...
Ingolme Posted November 10, 2007 Share Posted November 10, 2007 try this:var n = 0.5;n = n.toFixed(2); // outputs 0.50Now the number is fixed to two decimals Link to comment Share on other sites More sharing options...
Reg Edit Posted November 10, 2007 Share Posted November 10, 2007 try this:var n = 0.5;n = n.toFixed(2); // outputs 0.50Now the number is fixed to two decimalsGood point--bear in mind that rounding will then kick in, which is probably an extra benefit in this case, but may not always be desirable. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.