Skip to content

Instantly share code, notes, and snippets.

@Polkovn1k
Last active March 17, 2023 18:55
Show Gist options
  • Save Polkovn1k/bbc4d78663abd4ca1529c96b8ea3bd53 to your computer and use it in GitHub Desktop.
Save Polkovn1k/bbc4d78663abd4ca1529c96b8ea3bd53 to your computer and use it in GitHub Desktop.
Custom number to fix (JS)
Number.prototype.customNumberFix = function(fractionalPart) {
let split = String(this).split('.');
if (split.length > 1) {
let left = split[0];
let right = split[1].substr(0, (!fractionalPart ? 4 : fractionalPart));
return Number(left + (fractionalPart !== 0 ? `.${right}` : ``));
}
return Number(this);
}
//Use
(3223.320020).customNumberFix(2) //result 3223.32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment