Last active
March 17, 2023 18:55
-
-
Save Polkovn1k/bbc4d78663abd4ca1529c96b8ea3bd53 to your computer and use it in GitHub Desktop.
Custom number to fix (JS)
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
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