How to keep two decimal places in javascript (without rounding)

青灯夜游
Release: 2022-03-28 20:18:37
Original
9219 people have browsed it

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".

How to keep two decimal places in javascript (without rounding)

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
Copy after login

How to keep two decimal places in javascript (without rounding)

Method 2: Using the floor() function of the Math object

var a = 2.466;
var b = Math.floor(a*100)/100;
console.log(b);
Copy after login

How to keep two decimal places in javascript (without rounding)

More examples:

How to keep two decimal places in javascript (without rounding)

[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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template