简体   繁体   English

如何将输入转换为2个小数位?

[英]How can I convert to 2 decimal places on inputs?

How can i did an script to multiply 2 inputs and sum results my question is: "How can i do to put 2 decimals on each input? 我该如何执行一个脚本来将2个输入相乘并求和结果,我的问题是:“如何在每个输入上放置2个小数?

 soles * cost      = subtotal
 subtotal + dolares = total  

This is my view html 这是我的观点html

   <label>Sum Soles :</label>
   <input id="soles" value="2233.3234333" /> 

   <label>Sum Dolars :</label>
   <input id="dolars" value="3244.3566" />

   <br/><br/>
   <label>Cost of Dolar</label>
   <input type="text" id="cost" maxlength="5" onchange="doMath();" />

   <label>Total Soles to Dolars</label>
   <input id="subtotal" readonly="readonly" />

   <br/><br/>  
   <label>SUM TOTAL</label>
   <input id="total" readonly="readonly" />  

This is my script 这是我的剧本

<script type="text/javascript">
function doMath()
{
    // Capture the entered values of two input boxes
    var soles = document.getElementById('soles').value;
    var cost = document.getElementById('cost').value;
    var dolares=document.getElementById('dolars').value;

    // Add them together and display
    var subtotal = parseFloat(soles) * parseFloat(cost);
    document.getElementById('subtotal').value = subtotal;

    var total = parseFloat(subtotal) + parseFloat(dolars);
    document.getElementById('total').value = total;
}
</script>

I expect as result 我期望结果

Sum Soles : 2233.32
Sum Dolars 3244.36
Cost  : 1.2
Total Soles to dolars = 2679,98
Total :5924,34

Please somebody can help me with this? 请有人可以帮助我吗?

I will appreciate help 我将不胜感激

You can use the number.toFixed(2) prototype. 您可以使用number.toFixed(2)原型。

Edit : This will return 1.20 for Costo and not 1.2 . 编辑 :这将为Costo返回1.20 ,而不是1.2 If you wish to round to 2 decimals, you can use the Math.round method that tak3r provided . 如果希望舍入到两位小数,可以使用tak3r提供Math.round方法。

Here's a prototype that does the same thing: 这是做相同事情的原型:

Number.prototype.roundToDecimals = function(decimals) {
    decimals = decimals || 0;
    var pow = Math.pow(10, decimals);
    return Math.round(this * pow) / pow;
};

> (1.234567).roundToDecimals(2); 
1.23

> (1.2).roundToDecimals(2); 
1.2

you can use .toFixed(2) or mathematically Math.floor(number * 100) / 100 -> they are interchangeable but .toFixed() returns a string so keep that in mind 您可以使用.toFixed(2)或数学上的Math.floor(number * 100) / 100 >它们是可互换的,但是.toFixed()返回一个字符串,因此请记住

function doMath()
{
    // Capture the entered values of two input boxes
    var soles = document.getElementById('soles').value;
    var costo = document.getElementById('costo').value;
    var dolares=document.getElementById('dolares').value;

    // Add them together and display
    var subtotal = Math.floor(parseFloat(soles) * 100) / 100 * Math.floor(parseFloat(costo) * 100) / 100;
    document.getElementById('subtotal').value = subtotal.toFixed(2);

    var total = parseFloat(subtotal) + parseFloat(dolares);
    document.getElementById('total').value = total;
}

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

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