How to round float numbers in javascript?

JavascriptRounding

Javascript Problem Overview


I need to round for example 6.688689 to 6.7, but it always shows me 7.

My method:

Math.round(6.688689);
//or
Math.round(6.688689, 1);
//or 
Math.round(6.688689, 2);

But result always is the same 7... What am I doing wrong?

Javascript Solutions


Solution 1 - Javascript

Number((6.688689).toFixed(1)); // 6.7

Solution 2 - Javascript

var number = 6.688689;
var roundedNumber = Math.round(number * 10) / 10;

Solution 3 - Javascript

Use toFixed() function.

(6.688689).toFixed(); // equal to "7"
(6.688689).toFixed(1); // equal to "6.7"
(6.688689).toFixed(2); // equal to "6.69"

Solution 4 - Javascript

Upd (2019-10). Thanks to Reece Daniels code below now available as a set of functions packed in npm-package expected-round (take a look).


You can use helper function from MDN example. Than you'll have more flexibility:

Math.round10(5.25, 0);  // 5
Math.round10(5.25, -1); // 5.3
Math.round10(5.25, -2); // 5.25
Math.round10(5, 0);     // 5
Math.round10(5, -1);    // 5
Math.round10(5, -2);    // 5

Upd (2019-01-15). Seems like MDN docs no longer have this helper funcs. Here's a backup with examples:

// Closure
(function() {
  /**
   * Decimal adjustment of a number.
   *
   * @param {String}  type  The type of adjustment.
   * @param {Number}  value The number.
   * @param {Integer} exp   The exponent (the 10 logarithm of the adjustment base).
   * @returns {Number} The adjusted value.
   */
  function decimalAdjust(type, value, exp) {
    // If the exp is undefined or zero...
    if (typeof exp === 'undefined' || +exp === 0) {
      return Math[type](value);
    }
    value = +value;
    exp = +exp;
    // If the value is not a number or the exp is not an integer...
    if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
      return NaN;
    }
    // If the value is negative...
    if (value < 0) {
      return -decimalAdjust(type, -value, exp);
    }
    // Shift
    value = value.toString().split('e');
    value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp)));
    // Shift back
    value = value.toString().split('e');
    return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp));
  }

  // Decimal round
  if (!Math.round10) {
    Math.round10 = function(value, exp) {
      return decimalAdjust('round', value, exp);
    };
  }
  // Decimal floor
  if (!Math.floor10) {
    Math.floor10 = function(value, exp) {
      return decimalAdjust('floor', value, exp);
    };
  }
  // Decimal ceil
  if (!Math.ceil10) {
    Math.ceil10 = function(value, exp) {
      return decimalAdjust('ceil', value, exp);
    };
  }
})();

Usage examples:

// Round
Math.round10(55.55, -1);   // 55.6
Math.round10(55.549, -1);  // 55.5
Math.round10(55, 1);       // 60
Math.round10(54.9, 1);     // 50
Math.round10(-55.55, -1);  // -55.5
Math.round10(-55.551, -1); // -55.6
Math.round10(-55, 1);      // -50
Math.round10(-55.1, 1);    // -60
Math.round10(1.005, -2);   // 1.01 -- compare this with Math.round(1.005*100)/100 above
Math.round10(-1.005, -2);  // -1.01
// Floor
Math.floor10(55.59, -1);   // 55.5
Math.floor10(59, 1);       // 50
Math.floor10(-55.51, -1);  // -55.6
Math.floor10(-51, 1);      // -60
// Ceil
Math.ceil10(55.51, -1);    // 55.6
Math.ceil10(51, 1);        // 60
Math.ceil10(-55.59, -1);   // -55.5
Math.ceil10(-59, 1);       // -50

Solution 5 - Javascript

> +(6.688687).toPrecision(2)
6.7

A Number object in JavaScript has a method that does exactly what you need. That method is Number.toPrecision([precision]).

Just like with .toFixed(1) it converts the result into a string, and it needs to be converted back into a number. Done using the + prefix here.

simple benchmark on my laptop:

number = 25.645234 typeof number
50000000 x number.toFixed(1) = 25.6 typeof string / 17527ms
50000000 x +(number.toFixed(1)) = 25.6 typeof number / 23764ms
50000000 x number.toPrecision(3) = 25.6 typeof string / 10100ms
50000000 x +(number.toPrecision(3)) = 25.6 typeof number / 18492ms
50000000 x Math.round(number*10)/10 = 25.6 typeof number / 58ms
string = 25.645234 typeof string
50000000 x Math.round(string*10)/10 = 25.6 typeof number / 7109ms

Solution 6 - Javascript

If you not only want to use toFixed() but also ceil() and floor() on a float then you can use the following function:

function roundUsing(func, number, prec) {
    var tempnumber = number * Math.pow(10, prec);
    tempnumber = func(tempnumber);
    return tempnumber / Math.pow(10, prec);
}

Produces:

> roundUsing(Math.floor, 0.99999999, 3)
0.999
> roundUsing(Math.ceil, 0.1111111, 3)
0.112

UPD:

The other possible way is this:

Number.prototype.roundUsing = function(func, prec){
    var temp = this * Math.pow(10, prec)
    temp = func(temp);
    return temp / Math.pow(10, prec)
}

Produces:

> 6.688689.roundUsing(Math.ceil, 1)
6.7
> 6.688689.roundUsing(Math.round, 1)
6.7
> 6.688689.roundUsing(Math.floor, 1)
6.6

Solution 7 - Javascript

My extended round function:

function round(value, precision) {
  if (Number.isInteger(precision)) {
  	var shift = Math.pow(10, precision);
    // Limited preventing decimal issue
    return (Math.round( value * shift + 0.00000000000001 ) / shift);
  } else {
  	return Math.round(value);
  }
} 

Example Output:

round(123.688689)     // 123
round(123.688689, 0)  // 123
round(123.688689, 1)  // 123.7
round(123.688689, 2)  // 123.69
round(123.688689, -2) // 100
round(1.015, 2) // 1.02

Solution 8 - Javascript

There is the alternative .toLocaleString() to format numbers, with a lot of options regarding locales, grouping, currency formatting, notations. Some examples:


Round to 1 decimal, return a float:

const n = +6.688689.toLocaleString('fullwide', {maximumFractionDigits:1})
console.log(
  n, typeof n
)


Round to 2 decimals, format as currency with specified symbol, use comma grouping for thousands:

console.log(
  68766.688689.toLocaleString('fullwide', {maximumFractionDigits:2, style:'currency', currency:'USD', useGrouping:true})   
)


Format as locale currency:

console.log(
  68766.688689.toLocaleString('fr-FR', {maximumFractionDigits:2, style:'currency', currency:'EUR'})   
)


Round to minimum 3 decimal, force zeroes to display:

console.log(
  6.000000.toLocaleString('fullwide', {minimumFractionDigits:3})
)


Percent style for ratios. Input * 100 with % sign

console.log(
  6.688689.toLocaleString('fullwide', {maximumFractionDigits:2, style:'percent'})
)

Solution 9 - Javascript

See below

var original = 28.59;

var result=Math.round(original*10)/10 will return you returns 28.6

Hope this is what you want..

Solution 10 - Javascript

I have very good solution with if toFixed() is not working.

function roundOff(value, decimals) {
  return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}

Example

roundOff(10.456,2) //output 10.46

Solution 11 - Javascript

float(value,ndec);
function float(num,x){
this.num=num;
this.x=x;
var p=Math.pow(10,this.x);
return (Math.round((this.num).toFixed(this.x)*p))/p;
}

Solution 12 - Javascript

I think this function can help.

 function round(value, ndec){
	var n = 10;
	for(var i = 1; i < ndec; i++){
		n *=10;
	}

	if(!ndec || ndec <= 0)
		return Math.round(value);
	else
		return Math.round(value * n) / n;
}


round(2.245, 2) //2.25
round(2.245, 0) //2

Solution 13 - Javascript

if you're under node.js context, you can try mathjs

const math = require('mathjs')
math.round(3.1415926, 2) 
// result: 3.14

Solution 14 - Javascript

+((6.688689 * (1 + Number.EPSILON)).toFixed(1)); // 6.7
+((456.1235 * (1 + Number.EPSILON)).toFixed(3)); // 456.124

Solution 15 - Javascript

Math.round((6.688689 + Number.EPSILON) * 10) / 10

Solution stolen from https://stackoverflow.com/a/11832950/2443681

This should work with nearly any float value. It doesn't force decimal count though. It's not clear whether this was a requirement. Should be faster than using toFixed(), which has other issues as well based on the comments to other answers.

A nice utility function to round in needed decimal precision:

const roundToPrecision = (value, decimals) => {
  const pow = Math.pow(10, decimals);
  return Math.round((value + Number.EPSILON) * pow) / pow;
};

Solution 16 - Javascript

I think below function can help

function roundOff(value,round) {
   return (parseInt(value * (10 ** (round + 1))) - parseInt(value * (10 ** round)) * 10) > 4 ? (((parseFloat(parseInt((value + parseFloat(1 / (10 ** round))) * (10 ** round))))) / (10 ** round)) : (parseFloat(parseInt(value * (10 ** round))) / ( 10 ** round));
}

usage : roundOff(600.23458,2); will return 600.23

Solution 17 - Javascript

Minor tweak to this answer:

function roundToStep(value, stepParam) {
   var step = stepParam || 1.0;
   var inv = 1.0 / step;
   return Math.round(value * inv) / inv;
}

roundToStep(2.55, 0.1) = 2.6
roundToStep(2.55, 0.01) = 2.55
roundToStep(2, 0.01) = 2

Solution 18 - Javascript

If you're using Browserify today, you're going to have to try: roundTo a very useful NPM lib

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVitalii PonomarView Question on Stackoverflow
Solution 1 - JavascriptdavinView Answer on Stackoverflow
Solution 2 - JavascriptfivedigitView Answer on Stackoverflow
Solution 3 - JavascriptrpeshkovView Answer on Stackoverflow
Solution 4 - Javascripta.s.panchenkoView Answer on Stackoverflow
Solution 5 - JavascriptEvgenyView Answer on Stackoverflow
Solution 6 - JavascriptDennis YarmoshView Answer on Stackoverflow
Solution 7 - JavascriptNick TsaiView Answer on Stackoverflow
Solution 8 - JavascriptNVRMView Answer on Stackoverflow
Solution 9 - JavascriptFahim ParkarView Answer on Stackoverflow
Solution 10 - JavascriptAshish GondaliyaView Answer on Stackoverflow
Solution 11 - JavascriptCelestin SvView Answer on Stackoverflow
Solution 12 - JavascriptJefferson RibeiroView Answer on Stackoverflow
Solution 13 - JavascripthexcolaView Answer on Stackoverflow
Solution 14 - JavascriptThanh Nguyễn ChíView Answer on Stackoverflow
Solution 15 - JavascriptMakke R.View Answer on Stackoverflow
Solution 16 - JavascriptKRISHNA TEJAView Answer on Stackoverflow
Solution 17 - JavascriptCrashalotView Answer on Stackoverflow
Solution 18 - JavascriptpeterbView Answer on Stackoverflow