Nodejs Number Calculate reprLocale()

Here you can find the source of reprLocale()

Method Source Code

Number.prototype.reprLocale = function () {
    var result,//from w  w  w . ja v a2  s.  com
        digits = (arguments.length > 0)? arguments[0] : undefined,
        number = String(this.repr(digits)),
        int = number.split(".")[0],
        intparts = [],
        float = (number.indexOf(".") >= 0)? number.split(".")[1] : undefined;
    while (int.length > 0) {
        if (int.length <= 3) {
            intparts.unshift(int);
            int = "";
        } else {
            intparts.unshift(int.substr(-3,3));
            int = int.substr(0, int.length-3);
        }
    }
    result = intparts.join(Locale.separator.thousand);
    if (float) {
        result += Locale.separator.digit + float;
    }
    return result;
};

Related

  1. powerOf(power)
    Number.prototype.powerOf = function(power) {
      return Math.pow(this, power);
    };
    
  2. randStr()
    Number.prototype.randStr = function() {
        var alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
        var res = '';
        for(var i = 0; i < this; i++) {
            res += alpha[Math.floor(Math.random() * alpha.length)];
        return res;
    
  3. range(a, b, i)
    Number.prototype.range  = function (a, b, i) {
        i = i || true;
        var min = Math.min.apply(Math, [a, b]),
            max = Math.max.apply(Math, [a, b]);
        return i ? this >= min && this <= max : this > min && this < max;
    };
    
  4. repeat(n)
    Number.prototype.repeat = function(n){
      return this.toString().repeat(n);
    
  5. repr()
    Number.prototype.repr = function () {
        var digits = (arguments.length > 0)? arguments[0] : undefined;
        if (typeof digits !== "number") {
            return Number(this);
        if (digits <= 0) {
            return Math.round(this);
        var factor = Math.pow(10, digits);
    ...
    
  6. reverse()
    Number.prototype.reverse = function() {
      return parseInt(this.toString().split('').reverse().join(''), 10);
    
  7. rnd2()
    Number.prototype.rnd2 = function () {
      return Math.round(this.valueOf() * 100) / 100;
    
  8. rotate()
    Number.prototype.rotate = Number.prototype.rotate || function () {
      return Math.floor((this / 10) + ((this % 10) * Math.pow(10, exports.digitLength(this) - 1)));
    };
    
  9. safeDivideBy(x)
    Number.prototype.safeDivideBy = function(x) {
        var result = this.valueOf() / x;
        if(result === Infinity) {
            return 0;
        else {
            return result;