javascript round to 2 decimal places
var number = 12.3456789;
var rounded = Math.round( number * 10 ) / 10;
// rounded is 12.3var numb = 123.23454;
numb = numb.toFixed(2);var subTotal="12.1345";// can also be int, float, string
var subTotalFormatted=parseFloat(subTotal).toFixed(2); //"12.13"
let money = 1.6;
money.toFixed(2); // 1.60
var num = 2;
var roundedString = num.toFixed(2);// 2.00function round(num, places) {
num = parseFloat(num);
places = (places ? parseInt(places, 10) : 0)
if (places > 0) {
let length = places;
places = "1";
for (let i = 0; i < length; i++) {
places += "0";
places = parseInt(places, 10);
}
} else {
places = 1;
}
return Math.round((num + Number.EPSILON) * (1 * places)) / (1 * places)
}
round(1.005, 2); // 1.01
round(1.005, "1"); // 1
round("1.23", 1); // 1.2
round("1.436", "2"); // 1.44
Also in JavaScript:
- javascript split string into array by comma and space
- javascript for loop return index
- unshift method in javascript
- extract all link with javascript
- chartts js 2 y axes label
- how to get json file in javascript
- vuex-module-decorators nuxt axios
- javascript find object in array and replace it
- jquery select element with data
- ajax stand for
- how to get selected row index of table in javascript
- push values to state array class react
- get last date of a month javascript
- user input through js
- sass variables across files
- react chrome extension
- javascript foreach in object
- Angular comment
- js array to json
- javascript typewriter effect
- get syntethicbaseevent and parameter in react
- encrypt and decrypt in nodejs
- n JavaScript, select your <h1> element and give it some text
- How to get row index and cellindex together in javascript