乘以12 - js - 使用货币格式

时间:2017-05-27 21:22:53

标签: javascript calculator multiplying

我试图将计算器的结果乘以12来计算"年度"数字。

我不确定如何以保持小数点的方式编写函数:

HTML:

<p>Check Payments Per Month: <input type="text" class="hours"/></p>
<p>Cost per Payment: <input type="text" class="rate"/></p>
<input type="button" class="cbut" value="Calculate"/>
<p>Total Monthly Savings: $ <span class="total"></span></p>
<p>Total Annual Savings: $ <span class="totalyr"></span></p>

JS:

jQuery(document).ready(function($){
var $xhours = $('.hours');
var $xrate = $('.rate');
var $xcbut = $('.cbut');
var $xtotal = $('.total');
var $ytotal = $('.totalyr');

$xcbut.click(function()
{
    $xtotal.text(function()
    {
        var total = ($xhours.val() * $xrate.val());

        return total.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,');
    });

     $ytotal.text(function()
    {



    });


});
});

在这里工作JS:https://jsfiddle.net/j52zsc2b/16/

1 个答案:

答案 0 :(得分:0)

您可以使用Math.ceil,如下所示:

// precision is 10 for 10ths, 100 for 100ths, etc.
function roundUp(num, precision) {
  return Math.ceil(num * precision) / precision
}

roundUp(192.168, 10) //=> 192.2