最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Removing last two Zeros [AutoNumeric.js, JQuery] - Stack Overflow

programmeradmin2浏览0评论

I'm using autonumeric.js to generate currency number format, the problem is autonumeric's generating 2 more zeros after comma. e.g 40560000 became 40.560.000,00

I want to remove the last 2 zeros, so instead of 40.560.000,00 the result of autonumeric will be 40.560.000

This is my script :

$('td.sub_total').autoNumeric('init', {aSep: '.', aDec: ','});
$('td.vat').autoNumeric('init', {aSep: '.', aDec: ','});
$('td.total').autoNumeric('init', {aSep: '.', aDec: ','});

Any help will be much appreciated, thank you.

I'm using autonumeric.js to generate currency number format, the problem is autonumeric's generating 2 more zeros after comma. e.g 40560000 became 40.560.000,00

I want to remove the last 2 zeros, so instead of 40.560.000,00 the result of autonumeric will be 40.560.000

This is my script :

$('td.sub_total').autoNumeric('init', {aSep: '.', aDec: ','});
$('td.vat').autoNumeric('init', {aSep: '.', aDec: ','});
$('td.total').autoNumeric('init', {aSep: '.', aDec: ','});

Any help will be much appreciated, thank you.

Share Improve this question edited Jul 28, 2016 at 13:13 Rajesh 24.9k5 gold badges50 silver badges83 bronze badges asked Jul 28, 2016 at 13:09 M AnsyoriM Ansyori 4888 silver badges22 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 7

According to the documentation, you can simply use the mDec key in the object.

Example:

$('td.sub_total').autoNumeric('init', {aSep: '.', aDec: ',', mDec: '0'});
$('td.vat').autoNumeric('init', {aSep: '.', aDec: ',', mDec: '0'});
$('td.total').autoNumeric('init', {aSep: '.', aDec: ',', mDec: '0'});

As mentioned in a comment by Alex:

for autoNumeric v4+ the proper way to do this is to use:

{decimalPlaces:'0'}

autonumeric.js provide option (aPad) to remove unneccessary zero numbers. You can try this flow the source code above. You can try:

    // wacth change opts and re-instance autoNumeric
    scope.$watch(() => {
        return $(element).attr('kv-auto-numeric'); // set a watch on the actual DOM value
    },
    (val: string) => {
        opts = angular.extend({}, this.options, scope.$eval(val)) as AutoNumericOptions;

        // remove unneccessary zero numbers
        opts.aPad = false;
        element.autoNumeric('update', opts);
    });

Autonumeric contains an option called "allowDecimalPadding". If this option is set as true, padding is only done when there are some decimals.

Autonumeric npm.js

发布评论

评论列表(0)

  1. 暂无评论