Deadmano
Deadmano

Reputation: 237

Adding toFixed To Existing Math.abs Function

I have a function I've been playing around with to try and display certain larger cardinal numbers with a text suffix. However I would like to expand upon it so that I can call toFixed to omit all decimals (toFixed(0)). I have tried all possible combinations and on the variable itself but I can't seem to get it to work...

The function I have that I want no decimals to display is:

function cardinalNumbers(numericalValue) {
// Twenty Four Zeroes For Septillions.

return Math.abs(Number(numericalValue)) >= 1.0e+24
   ? Math.abs(Number(numericalValue)) / 1.0e+24 + "S"

   // Twnty One Zeroes For Sextillions.
   : Math.abs(Number(numericalValue)) >= 1.0e+21
   ? Math.abs(Number(numericalValue)) / 1.0e+21 + "s"

   // Eighteen Zeroes For Quintillions.
   : Math.abs(Number(numericalValue)) >= 1.0e+18
   ? Math.abs(Number(numericalValue)) / 1.0e+18 + "Q"

   // Fifteen Zeroes For Quadrillion.
   : Math.abs(Number(numericalValue)) >= 1.0e+15
   ? Math.abs(Number(numericalValue)) / 1.0e+15 + "q"

   // Twelve Zeroes For Trillions.
   : Math.abs(Number(numericalValue)) >= 1.0e+12
   ? Math.abs(Number(numericalValue)) / 1.0e+12 + "T" 

   // Nine Zeroes For Billions.
   : Math.abs(Number(numericalValue)) >= 1.0e+9
   ? Math.abs(Number(numericalValue)) / 1.0e+9 + "B"

   // Six Zeroes For Millions.
   : Math.abs(Number(numericalValue)) >= 1.0e+6
   ? Math.abs(Number(numericalValue)) / 1.0e+6 + "M"

   // Three Zeroes For Thousands.
   : Math.abs(Number(numericalValue)) >= 1.0e+3
   ? Math.abs(Number(numericalValue)) / 1.0e+3 + "K"

   : Math.abs(Number(numericalValue));
}

An example would be 227155515434853440 formats as 227q.

Upvotes: 0

Views: 1124

Answers (3)

sawan kumar
sawan kumar

Reputation: 1

ntg like that, ur div of x is overlapping,,, first 3 have the value of 1st, so rewrite the code

Upvotes: 0

Teepeemm
Teepeemm

Reputation: 4508

The answer to your original question is that you need to call .toFixed(0) after the division, but before adding the suffix:
( Math.abs(Number(numericalValue)) / 1.0e+9 ).toFixed(0) + "B"

But you keep doing the same thing. That’s screaming for a loop of some sort.

function cardinalNumbers(numericalValue) {
    var suffixes = ['','K','M','B','T','q','Q','s','Q'];
    var x = Math.abs(Number(numericalValue));
    var sign = numericalValue / x;
    var index = 0;
    while ( x >= 1.0e3 ) {
        x /= 1.0e3;
        index++;
    }
    x *= sign;
    return x.toFixed(0)+suffixes[index];
}

This will run into problems if your numbers are too big. You'll either need to break out of the loop, or decide on some suffixes for larger numbers.

Upvotes: 1

Jerry
Jerry

Reputation: 71538

You would have to call .toFixed(0) at every point where a value is being returned and maybe a variable like Bartek suggested:

function cardinalNumbers(numericalValue) {

    var x = Math.abs(Number(numericalValue));
    // Twenty Four Zeroes For Septillions.
    return x >= 1.0e+24
    ? (x / 1.0e+24).toFixed(0) + "S"

    // Twnty One Zeroes For Sextillions.
    : x >= 1.0e+21
    ? (x / 1.0e+21).toFixed(0) + "s"

    // Eighteen Zeroes For Quintillions.
    : x >= 1.0e+18
    ? (x / 1.0e+18).toFixed(0) + "Q"

    // Fifteen Zeroes For Quadrillion.
    : x >= 1.0e+15
    ? (x / 1.0e+15).toFixed(0) + "q"

    // Twelve Zeroes For Trillions.
    : x >= 1.0e+12
    ? (x / 1.0e+12).toFixed(0) + "T" 

    // Nine Zeroes For Billions.
    : x >= 1.0e+9
    ? (x / 1.0e+9).toFixed(0) + "B"

    // Six Zeroes For Millions.
    : x >= 1.0e+6
    ? (x / 1.0e+6).toFixed(0) + "M"

    // Three Zeroes For Thousands.
    : x >= 1.0e+3
    ? (x / 1.0e+3).toFixed(0) + "K"

    : x.toFixed(0);
}

Or if you can use an array and want to use the same construct, you could use .toFixed(0) once. But I don't like using ternary operators for that much branching, so I would rather use if-elseif-else blocks:

function cardinalNumbers(numericalValue) {
    var result = [];
    var x = Math.abs(Number(numericalValue));
    if (x >= 1.0e+24) {
        // Twenty Four Zeroes For Septillions.
        result = [x / 1.0e+24, "S"];
    } else if (x >= 1.0e+21) {
        // Twenty One Zeroes For Sextillions.
        result = [x / 1.0e+21, "s"];
    } else if (x >= 1.0e+18) {
        // Eighteen Zeroes For Quintillions.
        result = [x / 1.0e+18, "Q"];
    } else if (x >= 1.0e+15) {
        // Fifteen Zeroes For Quadrillion.
        result = [x / 1.0e+15, "q"];
    } else if (x >= 1.0e+12) {
        // Twelve Zeroes For Trillions.
        result = [x / 1.0e+12, "T"];
    } else if (x >= 1.0e+9) {
        // Nine Zeroes For Billions.
        result = [x / 1.0e+9, "B"];
    } else if (x >= 1.0e+6) {
        // Six Zeroes For Millions.
        result = [x / 1.0e+6, "M"];
    } else if (x >= 1.0e+3) {
        // Three Zeroes For Thousands.
        result = [x / 1.0e+3, "K"];
    } else {
        result = [x, ""];
    }
    return result[0].toFixed(0) + result[1];
}

Upvotes: 2

Related Questions