简体   繁体   English

JavaScript上的价格计算字段错误

[英]Errors with price calculation fields on javascript

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 € 示例:获得价格1368.5€但需要此显示为1368.50€

can anybody help me with this issue? 有人可以帮我解决这个问题吗?

您可以编写sum.toFixed(2)来获得始终始终精确地sum.toFixed(2)两位小数的字符串。

Try 尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM