Skip to content

Instantly share code, notes, and snippets.

@viko16
Created June 20, 2019 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viko16/12590e5067731c5b9b85882b5ff2b00d to your computer and use it in GitHub Desktop.
Save viko16/12590e5067731c5b9b85882b5ff2b00d to your computer and use it in GitHub Desktop.
获取一个数字的小数部分
/**
* 获取一个数字的小数部分,如果是整数则返回 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