如何在Accounting.js中将小数点添加到小数?

时间:2018-07-03 10:23:27

标签: jquery accounting.js

我正在使用销售点,并且正在使用accounting.js来格式化付款模块中要支付的金额编号。

显然,我希望该字段具有浮点值,所以我有这个虚拟数字键盘,当单击该数字键盘时,它将像下面这样串联每个按钮的值:

enter image description here

但是当我单击小数点按钮时,它不起作用。到目前为止,这是我尝试过的,不确定我缺少什么。

// Payment Numpad  
$('section.payment-numpad .number-char').click(function(){

    let num = $(this).data('action');
    let defaultAmt = $('td.col-tendered.edit').text().replace(/,/g, "");
    let tendered;
    if(defaultAmt === "0.00"){
        tendered = num;
    }else{
        tendered = defaultAmt + num;
    }

    $('td.col-tendered.edit').text(accounting.formatNumber(parseFloat(tendered)));
    return false;
});

我还尝试了Accounting.js中的.toFixed(2),但仍然无法正常工作。

更新:以下是video供参考

2 个答案:

答案 0 :(得分:0)

在文档中。要么全局更改设置...

// Settings object that controls default parameters for library methods:
accounting.settings = {
    currency: {
        symbol : "$",   // default currency symbol is '$'
        format: "%s%v", // controls output: %s = symbol, %v = value/number (can be object: see below)
        decimal : ".",  // decimal point separator
        thousand: ",",  // thousands separator
        precision : 2   // decimal places
    },
    number: {
        precision : 0,  // default precision on numbers is 0
        thousand: ",",
        decimal : "."
    }
}

或在本地更改设置...

// Standard usage and parameters (returns string):
accounting.formatNumber(number, [precision = 0], [thousand = ","], [decimal = "."])

答案 1 :(得分:0)

尝试一下,希望对您有所帮助:)

function operator(amt, num) {
    let val = '';
    switch (num) {
     case '.':
      if (parseFloat(amt) == 0) {
       val = '0.';
      } else {
       if (amt.indexOf('.') > -1) {
        val = amt;
       } else {
        val = amt + '.';
       }
      }
      break;
    }
    return val;
   }

   $('section.payment-numpad .number-char').click(function () {
    let that = $(this),
            data = that.data(),
            num = data.action;

    let defaultAmt = $('.col-tendered.edit').data('value');
    let tendered;

    if (isNaN(parseFloat(defaultAmt))) {
     defaultAmt = 0;
    }

    if (that.hasClass('operator')) {
     tendered = operator(defaultAmt, num);
    } else {
     tendered = defaultAmt + '' + num;
    }

    $('.value').text(tendered);
    $('.col-tendered.edit').attr('data-value', tendered).data('value', tendered);
    var precision = 2;
    /*if (tendered != '') {
     var s = tendered.split('.');
     if (s[1] != undefined) {
      precision = s[1].length;
     }
    }*/
    $('.col-tendered.edit').text(accounting.formatNumber(parseFloat(tendered), precision));
    return false;
   });
table {
  width: 100%;				
}
.number-char {
  padding: 10px;
  background: #000;
  color: #fff;
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script src="http://openexchangerates.github.io/accounting.js/accounting.min.js"></script>
  <section class="payment-numpad">
   <div class="col-tendered edit" data-value=""></div>
   <!--<div>Input Value: <span class="value"></span></div>-->
   <table>
    <tr>
     <td class="number-char" data-action="1">1</td>
     <td class="number-char" data-action="2">2</td>
     <td class="number-char" data-action="3">3</td>
    </tr>
    <tr>
     <td class="number-char" data-action="4">4</td>
     <td class="number-char" data-action="5">5</td>
     <td class="number-char" data-action="6">6</td>
    </tr>
    <tr>
     <td class="number-char" data-action="7">7</td>
     <td class="number-char" data-action="8">8</td>
     <td class="number-char" data-action="9">9</td>
    </tr>
    <tr>
     <td class="number-char" data-action="0">0</td>
     <td class="number-char operator" data-action=".">.</td>
    </tr>
   </table>
  </section>

使用precision设置小数点后的固定值

var precision = 2;
$('.col-tendered.edit').text(accounting.formatNumber(parseFloat(tendered), precision));