9

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.

4 Answers 4

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'});
4
  • Well, I didn't read the documentation you mentioned above, thanks, it works :D
    – M Ansyori
    Commented Jul 28, 2016 at 13:34
  • mDec is the decimal place override. Using vMin/vMax is the primary way to set the number of decimals you want. In your case you should just use integers in those two options, and no decimals will appear.
    – Alex
    Commented Jan 11, 2017 at 8:34
  • 2
    With AutoNumeric v4 onward, decimalPlaceOverride has been dropped in favor of a more explicit way to specify the number of decimal places : decimalPlaces. That's it ;)
    – Alex
    Commented Jul 23, 2017 at 0:36
  • See mDec, vMin, vMax, and aPad from: decorplanit.com/plugin You can also attribute you're elements with those data attributes rather than having to defined different autoNumerics. For example a min and a max with up to 4 decimal precision points and to not pad with 0s: <input type="text" class="form-control" data-v-min="-9999999999999.9999" data-v-max="9999999999999.9999" data-a-pad="false"> Commented Aug 21, 2019 at 15:27
6

As mentioned in a comment by Alex:

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

{decimalPlaces:'0'}
2

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);
    });
3
  • 2
    From Review: Hi, please don't answer just with source code. Try to provide a nice description about how your solution works. See: How do I write a good answer?. Thanks Commented Jan 24, 2019 at 10:06
  • 1
    autonumeric.js provide option (aPad) to remove unneccessary zero numbers. You can try this flow the source code above.
    – K.Dinh
    Commented Jan 24, 2019 at 10:58
  • 1
    Please add all explanation to the answer itself, not to the comment section
    – Nico Haase
    Commented Jan 24, 2019 at 11:31
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.