Created
April 15, 2021 09:01
-
-
Save shhider/a5ce063526fe218f70b3924a0b8ad066 to your computer and use it in GitHub Desktop.
[isNumberOverPrecison]
This file contains 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
function isNumberOverPrecison(num: number, precision = 15) { | |
if (!isFinite(num)) { | |
return false; | |
} | |
let numStr = num.toString().toLowerCase(); | |
if (numStr.length <= 15) { | |
return false; | |
} | |
const expIdx = numStr.indexOf('e'); | |
if (expIdx > -1) { | |
numStr = numStr.substring(0, expIdx); | |
} | |
if (numStr.length <= 15) { | |
return false; | |
} | |
let count = 0; | |
for (let idx = 0; idx < numStr.length; idx++) { | |
const code = numStr.charCodeAt(idx); | |
if ( | |
code >= 48 && code <= 57 | |
&& (count > 0 || code !== 48) | |
) { | |
if (count >= precision) { | |
return true; | |
} | |
count += 1; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment