Skip to content

Instantly share code, notes, and snippets.

@soachishti
Created October 7, 2019 11:38
Show Gist options
  • Save soachishti/e6dac3c8159d138c5b865cfd5c687ccb to your computer and use it in GitHub Desktop.
Save soachishti/e6dac3c8159d138c5b865cfd5c687ccb to your computer and use it in GitHub Desktop.
Get Scale and Precision from Number - JavaScript
// 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