Skip to content

Instantly share code, notes, and snippets.

@shhider
Created April 15, 2021 09:01
Show Gist options
  • Save shhider/a5ce063526fe218f70b3924a0b8ad066 to your computer and use it in GitHub Desktop.
Save shhider/a5ce063526fe218f70b3924a0b8ad066 to your computer and use it in GitHub Desktop.
[isNumberOverPrecison]
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