toFixed、Math.round 的区别(转载)
区别 转载 round Math
2023-09-11 14:21:28 时间
转载 https://blog.csdn.net/qq_39571197/article/details/87597062
1、定义和用法,都是对数字进行四舍五入操作
Math.round()方法,可把一个数字舍入为最接近的整数。
toFixed()方法,可把 Number 四舍五入为指定小数位数的数字。
2、返回值的类型不同
-
const num = 123;
-
console.log(typeof(num.toFixed())); // "string"
-
console.log(typeof(Math.round(num))); // "number"
Math.round()方法,返回值为数字。
toFixed()方法,返回值为字符串。
3、处理精度不同
-
const num = 100.153;
-
console.log(num.toFixed(2)); // 100.15
-
console.log(num.toFixed(1)); // 100.1
-
console.log(num.toFixed()); // 100
-
-
console.log(Math.round(num)); // 100
Math.round()方法,就一个参数,而且用法也说明了,只返回整数。
toFixed()方法,可以接受第二个参数,用来规定小数位数,范围是2-20~
计算时需要 字符串转数字
parseFloat(((0.1 + 0.2)*10).toFixed(10))
Math.round 也可以保留任意位小数
相关文章
- 差分放大电路单端输出和双端输出区别以及应用(转载)
- 复盘:DFS与BFS的主要区别,在思想上的区别,代码实现上的区别
- C++中重载与重写函数区别及虚函数(转载)
- vue中使用go()和back()两种返回上一页的区别
- OLTP和OLAP的区别
- ReentrantLock和synchronized的区别随笔
- 央票和国债的区别
- msvc 库目录/附加库目录 包含目录/附加包含目录的区别
- NIO ByteBuffer的allocate与allocateDirect区别(HeapByteBuffer与DirectByteBuffer的区别)
- 线程的状态有哪些,线程中的start与run方法的区别
- 【转载】 大端模式和小端模式的区别是什么?
- 【转载】 nohup和&区别
- 【转载】 TensorFlow函数:tf.Session()和tf.Session().as_default()的区别
- Untiy-委托[delegate]和事件[event]详解和区别(二)
- oracle共享与专用模式的动态转换及区别(转载)
- jar 包与 war 包区别
- js跳出循环的方法区别(break,continue,return)(转载)
- js中const,var,let区别(转载)
- 转载-asp.net id 和name的区别
- 异步和多线程的区别