EDIT 2:
Use the Number
object’s toFixed
method like this:
var num = Number(0.005) // The Number() only visualizes the type and is not needed
var roundedString = num.toFixed(2);
var rounded = Number(roundedString); // toFixed() returns a string (often suitable for printing already)
It rounds 42.0054321 to 42.01
It rounds 0.005 to 0.01
It rounds -0.005 to -0.01 (So the absolute value increases on rounding at .5 border)
jsFiddle example
Related Contents:
- Javascript: formatting a rounded number to N decimals
- Javascript: Round up to the next multiple of 5
- How can I round down a number in Javascript?
- Formatting a number with exactly two decimals in JavaScript
- How to force JS to do math instead of putting two strings together [duplicate]
- Evaluating a string as a mathematical expression in JavaScript
- JavaScript % (modulo) gives a negative result for negative numbers
- How to find prime numbers between 0 – 100?
- How to round up a number in Javascript?
- JavaScript math, round to two decimal places [duplicate]
- What does the ^ (caret) symbol do in JavaScript?
- How to transform black into any given color using only CSS filters
- How to perform an integer division, and separately get the remainder, in JavaScript?
- What is the fastest factorial function in JavaScript? [closed]
- How can I round a number in JavaScript? .toFixed() returns a string?
- How can I remove the decimal part from JavaScript number?
- Remove insignificant trailing zeros from a number?
- JavaScript exponents
- Read/Write bytes of float in JS
- Get decimal portion of a number with JavaScript
- Is there a way to round numbers into a reader friendly format? (e.g. $1.1k) [closed]
- How to round float numbers in javascript?
- Why is math.max() returning NaN on an array of integers?
- What’s the algorithm to calculate aspect ratio?
- Gaussian/banker’s rounding in JavaScript
- JavaScript Number Split into individual digits
- Calculate date from week number in JavaScript
- Is there a way to calculate 3D rotation on X and Y axis from a 4×4 matrix
- In jQuery, what’s the best way of formatting a number to 2 decimal places?
- How do I round a number in JavaScript?
- Round number up to the nearest multiple of 3
- Adding to Number.MAX_VALUE
- Mixing two colors “naturally” in javascript
- Avoiding problems with JavaScript’s weird decimal calculations
- How do you round to one decimal place in JavaScript?
- How to round up a number to a precision of tenths in Javascript?
- In JavaScript, why does zero divided by zero return NaN, but any other divided by zero return Infinity?
- Convert 2:1 equirectangular panorama to cube map
- How can I convert numbers into scientific notation?
- Calculating the shortest route between two points
- Javascript Math Error: Inexact Floats [duplicate]
- How can I get sin, cos, and tan to use degrees instead of radians?
- How does Math.random() work in javascript?
- Write a truly inclusive random method for javascript
- How to simplify a decimal into the smallest possible fraction?
- How can I round to whole numbers in JavaScript?
- Why does adding two decimals in Javascript produce a wrong result? [duplicate]
- Show a leading zero if a number is less than 10 [duplicate]
- Round a float up to the next integer in javascript
- parseFloat rounding