Numeral-js: how to avoid rounding?

Created on 30 May 2017  路  5Comments  路  Source: adamwdraper/Numeral-js

var string = numeral(2.567).format('0,0.00');
// '2.57'

I don't want a rounding,I need to throw away the last digit,so the result should be 2.56.
so,how should I config it?

Most helpful comment

could you add an option to config the rounding behavior?

All 5 comments

I don't think you're going to be able to do that with just numeral. You could use some sort of helper function to round your number first (adapted from MDN example): https://jsfiddle.net/0cxwbdhw/1/

var myNamespace = {};

myNamespace.floor = function(number, precision) {
    var factor = Math.pow(10, precision);
    var tempNumber = number * factor;
    var roundedTempNumber = Math.floor(tempNumber);

    return roundedTempNumber / factor;
};

myNamespace.floor(2.567, 2); // 2.56

I'd be curious to know why you want to do this, though, since it's counter-intuitive to how rounding normally works.

could you add an option to config the rounding behavior?

Thanks, we're using it for displaying currency numbers, and we need to summarize numbers without having them rounded up.

try:

var string = numeral(2.567 - 0.005).format('0,0.00'); // '2.56'
var string = numeral(2.562 - 0.005).format('0,0.00'); // '2.56'
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ragulka picture ragulka  路  6Comments

MisterGoodcat picture MisterGoodcat  路  3Comments

amirtgm picture amirtgm  路  6Comments

leosco picture leosco  路  5Comments

Eric24 picture Eric24  路  8Comments