Home  >  Article  >  Web Front-end  >  js四舍五入数学函数round使用实例_基础知识

js四舍五入数学函数round使用实例_基础知识

WBOY
WBOYOriginal
2016-05-16 16:48:591213browse

js中的round函数可以用来对数字进行四舍五入处理,它针对的是小数点后面的第一位数字进行计算。
round函数可以对数字取整,它是一个四舍五入函数,下面来看看round的语法:

复制代码 代码如下:

Math.round(number)

下面来看几个范例:
复制代码 代码如下:

document.write(Math.round(2.65));// print 3
document.write(Math.round(7.05));// print 7
document.write(Math.round(-2.65));// print -3
document.write(Math.round(-8.15));// print -8
document.write(Math.round(11.65));// print 12

round函数的四舍五入只针对小数点后的第一位,如果要针对小数点后的其它位,我们可以先对数字乘以10的整数倍,round以后再除以同样的数:
复制代码 代码如下:

var my_val=11.257;
var my_val=11.254;
document.write(Math.round(my_val*100)/100);
//print 11.25
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