0

i have a problem with price calculation script:

this is the code:

function setVisibility(id, visibility) {
document.getElementById(id).style.display = visibility;
}


function getRBtnName(GrpName) {
  var sel = document.getElementsByName(GrpName);
  var fnd = -1;
  var str = '';
  for (var i=0; i<sel.length; i++) {
    if (sel[i].checked == true) { str = sel[i].value ;  fnd = i; }
  }


//  return fnd;   // return option index of selection
// comment out next line if option index used in line above  
  return str;
} 

function DisplayPrice(price){
  var val1 = getRBtnName('deckung').split(',');

  var sum = parseFloat(val1[0]+0);
  document.getElementById('totalSum').value=sum +",- €";

}
onload=function() {
  DisplayPrice();   
}

The problem is that i get the prices without a second place!

Example: get price 1368.5 € But need this to display as 1368.50 €

can anybody help me with this issue?

  • N.B.: you should make calculations in cents instead with floating point values (rounding errors) (in case you do any other calculations with the price). – Felix Kling Nov 02 '11 at 13:06
  • what do you mean don'T understand it ? – cominaction Nov 02 '11 at 13:12
  • If you only display prices, then you don't have to worry. I was just thinking it'd be worth to point out. More information here: [Precise Financial Calculation in JavaScript. What Are the Gotchas?](http://stackoverflow.com/questions/2876536/precise-financial-calculation-in-javascript-what-are-the-gotchas). – Felix Kling Nov 02 '11 at 13:15

2 Answers2

0

You can write sum.toFixed(2) to get a string that always has exactly two decimal places.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

Try

var num = new Number(val1[0]);
document.getElementById('totalSum').value = num.toFixed(2) +",- €";
Amit Gupta
  • 577
  • 4
  • 14