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

How to round data in JavaScript

清浅
清浅Original
2019-01-16 14:37:555434browse

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));

Rendering:

How to round data in JavaScript


##toFixed() method

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

Rendering:

How to round data in JavaScript

##parseInt() method

document.write(parseInt("12.333"));
	
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"));
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!

Statement:
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