Numeral-js: how to switch rounding off

Created on 30 Apr 2017  Â·  6Comments  Â·  Source: adamwdraper/Numeral-js

Hi, when I use the numeral I want to format the number to 0.00a but it's rounding the last digit, I don't want to do the rounding and I don't find any document about it.
value: 4986444.0
format: $0.00a
return: $4.99m
expected: $4.98m

Most helpful comment

numeral().format() accepts a 2nd parameter which is the rounding function to use.

In your case, you probably want Math.floor -
numeral(4986444.0).format('0.00a', Math.floor)

You can also use bitwise negation like this
numeral(4986444.0).format('0.00a', n => ~~n)

I hope this helps

All 6 comments

numeral().format() accepts a 2nd parameter which is the rounding function to use.

In your case, you probably want Math.floor -
numeral(4986444.0).format('0.00a', Math.floor)

You can also use bitwise negation like this
numeral(4986444.0).format('0.00a', n => ~~n)

I hope this helps

I failed to turn off rounding with n => n. Maybe I understand this rounding function wrongly?

numeral(123456789.1289).format(0,0.[00]); // returns "123,456,789.13"
numeral(123456789.1289).format(0,0.[00], n => n); // still returns "123,456,789.13"

Rounding still has to occur.

You can use Math.floor, Math.rand or Math.ceil, depending on the output you're looking for.

Whay is your specific use case? :)

Ok.

So currently:

numeral(123456789.1289).format(0,0.[00]); // returns "123,456,789.13"

But I want it to return 123,456,789.12 (".12" instead of rounding to ".13"), without much effort.

numeral.fn._format = numeral.fn.format;
numeral.fn.format = function (a, b) { return numeral.fn._format.call(this, a, b || numeral._.defaultRoundingFunction); };

numeral._.defaultRoundingFunction = Math.round;
numeral.setDefaultRoundingFunction = (fn) => { numeral._.defaultRoundingFunction = fn || numeral._.defaultRoundingFunction; }

image

some times, it‘s not work
image

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Eric24 picture Eric24  Â·  8Comments

zzzgit picture zzzgit  Â·  5Comments

GitzJoey picture GitzJoey  Â·  4Comments

garbinmarcelo picture garbinmarcelo  Â·  5Comments

ragulka picture ragulka  Â·  6Comments