Last active
August 6, 2021 22:32
-
-
Save t1mofe1/20fe0d8c8812dcc75607ddf9a96518fa to your computer and use it in GitHub Desktop.
Number.toFixed is rounding numbers, but this don't do that. It's just converting float to int.
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.toFixedNoRounding = function toFixedNoRounding(n = 0) { | |
if (n < 0) n = 0; | |
const str = this.toString(); | |
const dot = str.indexOf('.'); | |
if (dot === -1) return this; | |
return Number(str.slice(0, dot) + n !== 0 ? str.substr(dot, dot + n) : ''); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment