时间:2021-07-01 10:21:17 帮助过:16人阅读

首先我们来看一下toFixed()的基本语法
number.toFixed( num )
num表示小数点后显示的位数。
如果在toFixed()中没有指定参数,则它不会在小数位后面显示任何数字。
我们来看具体的示例
toFixed()中没有参数
代码如下
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
var test=213.73145;
document.write(test.toFixed());
</script>
</body>
</html>运行结果为:214
toFixed()中有参数
代码如下
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
var test=213.73145;
document.write(test.toFixed(3));
</script>
</body>
</html>运行结果为:213.731
使用toFixed()方法,其中数字是指数形式:toFixed()函数可用于将指数数字转换为小数位后具有特定位数。
代码如下
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
var test=2.13e+15;
document.write(test.toFixed(2));
</script>
</body>
</html>运行结果为:2130000000000000.00
本篇文章到这里就全部结束了,更多精彩内容大家可以关注Gxl网的其他相关栏目教程!!!
以上就是toFixed方法怎么使用的详细内容,更多请关注Gxl网其它相关文章!