javascript中的tofixed()方法用于使用定点表示法格式化数字,格式化数与到小数点右侧的特定的位数。也就是可以把 number 四舍五入为指定小数位数的数字,下面我们就来看一下tofixed()的具体方法。
首先我们来看一下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
本篇文章到这里就全部结束了,更多精彩内容大家可以关注的其他相关栏目教程!!!
以上就是tofixed方法怎么使用的详细内容。