Home >Web Front-end >Front-end Q&A >How to keep two decimal places in javascript (without rounding)
Implementation method: 1. Use the parseInt() and toFixed() functions, the syntax is "(parseInt(decimal value*100)/100).toFixed(2)"; 2. Use the floor() of the Math object Function, syntax "Math.floor(decimal value*100)/100".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
javascript Keep two decimal places (and not round)
In javascript, if you want to process a number, keep it without rounding Two decimal places. Here’s an introduction to the method:
Method 1: Using parseInt() and toFixed() functions
var a = 2.466; var b = ( parseInt( a * 100 ) / 100 ).toFixed(2); console.log(b); // 2.46
Method 2: Using the floor() function of the Math object
var a = 2.466; var b = Math.floor(a*100)/100; console.log(b);
More examples:
[Related recommendations: javascript video tutorial, web front-end]
The above is the detailed content of How to keep two decimal places in javascript (without rounding). For more information, please follow other related articles on the PHP Chinese website!