Created
October 7, 2019 11:38
-
-
Save soachishti/e6dac3c8159d138c5b865cfd5c687ccb to your computer and use it in GitHub Desktop.
Get Scale and Precision from Number - JavaScript
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
// Database Number: Scale and Precision | |
// Input: 123456.1234 | |
// Output: scale 4, precision 6 | |
function getScaleAndPrecision(x) { | |
x = parseFloat(x); | |
if (!x || isNaN(x)) return null; //NaN, undefined, null, 'abcd', ""... | |
let parts = x.toString().split("."); | |
if (parts.length > 2) return null; | |
return { | |
scale: parts[1] ? parts[1].length : 0, | |
precision: parts[0].length | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment