Created
June 20, 2019 13:04
-
-
Save viko16/12590e5067731c5b9b85882b5ff2b00d to your computer and use it in GitHub Desktop.
获取一个数字的小数部分
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 获取一个数字的小数部分,如果是整数则返回 0 | |
*/ | |
function getDecimal(num: number): number { | |
// 原理是先将数字乘以一定倍速,作为整数来计算 | |
// 具体乘多大视乎小数点后有多少位 (10为基数的 x 次幂) | |
const BASE = Math.pow(10, (num.toString().split('.')[1] || '').length); | |
return num * BASE % BASE / BASE; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment