M Ansyori
M Ansyori

Reputation: 488

Removing last two Zeros [AutoNumeric.js, JQuery]

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.

Upvotes: 9

Views: 4804

Answers (4)

Aswin Prasad
Aswin Prasad

Reputation: 417

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

Upvotes: 0

K.Dinh
K.Dinh

Reputation: 39

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);
    });

Upvotes: 2

ProllyGeek
ProllyGeek

Reputation: 15836

As mentioned in a comment by Alex:

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

{decimalPlaces:'0'}

Upvotes: 6

TrampolineTales
TrampolineTales

Reputation: 826

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'});

Upvotes: 7

Related Questions