Skip to content

Instantly share code, notes, and snippets.

@t1mofe1
Last active August 6, 2021 22:32
Show Gist options
  • Save t1mofe1/20fe0d8c8812dcc75607ddf9a96518fa to your computer and use it in GitHub Desktop.
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.
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