Skip to content

Instantly share code, notes, and snippets.

@st98
Last active August 29, 2015 14:05
Show Gist options
  • Save st98/b49055fe0b32647edec1 to your computer and use it in GitHub Desktop.
Save st98/b49055fe0b32647edec1 to your computer and use it in GitHub Desktop.
Number#length。何桁か調べます。
function log10(x) {
return Math.log(x) / Math.LN10;
}
Object.defineProperty(Number.prototype, 'length', {
get: function () {
return Math.floor(log10(Math.abs(this))) + 1;
},
set: function (value) {
return value;
},
configurable: true,
enumerable: false
});
// -----
Object.defineProperty(Number.prototype, 'length', {
get: function () {
return String(Math.floor(Math.abs(this))).length;
},
set: function (value) {
return value;
},
configurable: true,
enumerable: false
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment