Home > Web Front-end > JS Tutorial > How to round data in JavaScript

How to round data in JavaScript

清浅
Release: 2019-01-16 14:37:55
Original
5439 people have browsed it

There are several methods to implement data rounding: round method, tofixed method, parseInt method, ceil method and floor method

How to round data in JavaScript

Rounding of values ​​in JavaScript There are the following

round() methods: a number can be rounded to the nearest integer, that is, rounding

toFixed() method: a Number can be rounded to the specified number of decimal places number.

parseInt() method: can convert decimals into integers (bit operators)

ceil() method: can round up a number

floor() Method: A number can be rounded down

The specific usage of these methods will be introduced in detail in the article

round() method

	document.write(Math.round(4.55) + "<br>");
	document.write(Math.round(-6.45));
Copy after login

Rendering:

How to round data in JavaScript


##toFixed() method

var num=3.141592;
document.write(num.toFixed(2));
Copy after login

Rendering:

How to round data in JavaScript

##parseInt() method

document.write(parseInt("12.333"));
	
Copy after login
Rendering:


Image 17.jpg

ceil() method and floor() method

document.write("ceil方法:")
document.write(Math.ceil("12.333") + "<br>");
document.write("floor方法:")
document.write(Math.floor("12.333"));
Copy after login
Rendering:

The above is the detailed content of How to round data in JavaScript. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template