Last active
August 29, 2015 14:05
-
-
Save st98/b49055fe0b32647edec1 to your computer and use it in GitHub Desktop.
Number#length。何桁か調べます。
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
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