JAVA
JAVA

Reputation: 335

How to convert numbers to million?

As in image. for some values converting correctly but some of values not converting... you can see in image

I want to convert numbers to million.I am using Money format function to convert numbers but i am unable to convert numbers.

This is controller part.for some numbers it is converting to millions and for some numbers it is not converting.. Please someone help.

 $scope.MoneyFormat = function (labelValue) 
                    {
                          // Nine Zeroes for Billions
                          return Math.abs(Number(labelValue)) >= 1.0e+9

                               ? Math.abs(Number(labelValue)) / 1.0e+9 + "B"
                               // Six Zeroes for Millions 
                               : Math.abs(Number(labelValue)) >= 1.0e+6

                               ? Math.abs(Number(labelValue)) / 1.0e+6 + "M"
                               // Three Zeroes for Thousands
                               : Math.abs(Number(labelValue)) >= 1.0e+3

                               ? Math.abs(Number(labelValue)) / 1.0e+3 + "K"

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

Here I am converting numbers by using Moneyformat. This is controller part where I am converting numbers

            $scope.rep.won = $scope.MoneyFormat($scope.rep.won);
            $scope.outlook.rem = $scope.MoneyFormat($scope.outlook.rem);
            $scope.rep.expectedAmount = $scope.MoneyFormat($scope.rep.expectedAmount);
            $scope.rep.potential = $scope.MoneyFormat($scope.rep.potential);
            $scope.rep.quota = $scope.MoneyFormat($scope.rep.quota);

Upvotes: 33

Views: 47817

Answers (8)

Pratik Dev
Pratik Dev

Reputation: 434

I know I'm almost 6 years late at the party, but I can't help commenting after seeing so many long functions for this task.

You can use javascript's built-in method Intl (namespace for Internationalization API) here. Intl object has a function named NumberFormat where you can define your preferred language, notation type, and many more. in your use case, the code will look something like

const number = 234234;
const language = "en"

Intl.NumberFormat(language, {notation: "compact"}).format(number) //output - "234K"

check out MDN's doc about Intl and Intl.NumberFormat()

Upvotes: 14

Singh Aditya
Singh Aditya

Reputation: 1

The below Function will convert the number into readable format Example:

1 => 1
100 => 100
900 => 900
999 => 999

1000 => 1k
1200 => 1.2k
2600 => 2.6k
9999 => 9.9k

5000 => 5k


10000 => 10k
19999 => 19.9k
99999 => 9.k

100000 => 100k
905000 => 905k


1000000 => 1M

Code :

function formatNumberToReadableUnit(num){
    num = num.toString();
    switch(true){
        case (num.length < 4):
            return num;
        case (num.length >=4 && num.length <= 6):
            return (num/1000) + "K";
        case (num.length >= 7 && num.length <=9):
            return (num/1000000) + "M";
        default:
            return (num/1000000000)+"B";
    }
}

Upvotes: -1

Vikram Biwal
Vikram Biwal

Reputation: 2826

getNumber = function(num) {
    
    var units = ["","K","M","B","T","Q"]
    var unit = (num / 1.0e+1).toFixed(0).toString().length
    var r = unit%3
    var x =  Math.abs(Number(num))/Number('1.0e+'+(unit-r)).toFixed(2)
    return ((Math.round(x)==x) ? x : x.toFixed(2))+ ' ' + units[Math.floor(unit / 3)]
}
console.log(getNumber(680))
console.log(getNumber(68000))
console.log(getNumber(6800009))
console.log(getNumber(68000009))
console.log(getNumber(680000000))
console.log(getNumber(6800000000))
console.log(getNumber(68000000000))
console.log(getNumber(680000000000))
console.log(getNumber(6800000000000))
console.log(getNumber(68000000000000))
console.log(getNumber(680000000000000))
console.log(getNumber(6800000000000000))
console.log(getNumber(68000000000000000))
console.log(getNumber(680000000000000000))

Upvotes: 3

Meddie
Meddie

Reputation: 557

I was look for something to convert large numbers to billions. Maybe this can be of use to someone:

Number.prototype.toBillion = function(decimals) {
    decimals = typeof decimals !== "undefined" ? decimals : 2;
    return (this / 1000000000).toFixed(decimals);
};

let largeNumber = 100000000;
console.log(largeNumber.toBillion()); // Output: 0.1

Upvotes: 0

Jeff Weinberg
Jeff Weinberg

Reputation: 414

Improving upon the answer to include negatives:

function test(labelValue) {
  const sign = Math.sign(Number(labelValue));
  // Nine Zeroes for Billions
  return Math.abs(Number(labelValue)) >= 1.0e9
    ? sign * (Math.abs(Number(labelValue)) / 1.0e9) + "B"
    : // Six Zeroes for Millions
    Math.abs(Number(labelValue)) >= 1.0e6
    ? sign * (Math.abs(Number(labelValue)) / 1.0e6) + "M"
    : // Three Zeroes for Thousands
    Math.abs(Number(labelValue)) >= 1.0e3
    ? sign * (Math.abs(Number(labelValue)) / 1.0e3) + "K"
    : Math.abs(Number(labelValue));
}

alert(test(-99998000000)); 

Upvotes: 1

burgil
burgil

Reputation: 395

    getNumberUnit = function(num, round = 1) {
        const unit = Math.floor(Math.round(num / 1.0e+1).toLocaleString().replaceAll(',', '').length),
            wunit = ["Thousand","Million","Billion","Trillion","Quadrillion","Quintillion","Sextillion","Septillion","Octillion","Nonillion","Decillion","Undecillion","Duodecillion","Tredecillion","Quattuordecillion","Quindecillion","Sexdecillion","Septemdecillion","Octodecillion","Novemdecillion","Vigintillion","Unvigintillion","Duovigintillion","Trevigintillion","Quattuorvigintillion","Quinvigintillion","Sexvigintillion","Septvigintillion","Octovigintillion","Nonvigintillion","Trigintillion","Untrigintillion","Duotrigintillion"][Math.floor(unit / 3) - 1],
            funit = Math.abs(Number(num))/Number('1.0e+'+(unit-unit%3));
        return wunit ? funit.toFixed(round).toLocaleString() + ' ' + wunit : num.toFixed(round).toString();
    }

   var  result=getNumberUnit(764000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)
   
   console.log(result)

The best of all

Upvotes: 1

Steven Manuel
Steven Manuel

Reputation: 1978

I have no idea what $scope.MoneyFormat is.

So I simplified your function to a plain old js function and it works.

function convertToInternationalCurrencySystem (labelValue) {

    // Nine Zeroes for Billions
    return Math.abs(Number(labelValue)) >= 1.0e+9

    ? (Math.abs(Number(labelValue)) / 1.0e+9).toFixed(2) + "B"
    // Six Zeroes for Millions 
    : Math.abs(Number(labelValue)) >= 1.0e+6

    ? (Math.abs(Number(labelValue)) / 1.0e+6).toFixed(2) + "M"
    // Three Zeroes for Thousands
    : Math.abs(Number(labelValue)) >= 1.0e+3

    ? (Math.abs(Number(labelValue)) / 1.0e+3).toFixed(2) + "K"

    : Math.abs(Number(labelValue));

}

alert( convertToInternationalCurrencySystem (6800000) ); // this outputs 6.8M

JSFiddle: https://jsfiddle.net/r5ju34ey/

Upvotes: 90

Minenik
Minenik

Reputation: 1

Because of how far javascript number goes they can only go up to 1e301 This function shows the number in a 3 digit along with a letter, for example 30000000000000000 would be 30,0 Quadrillion and 1000000 would be 1,00 Million.

function simpleNumber(costOfIt, visualOfIt) {
    var visualOfIt = costOfIt.toString();

    var visualLeng = 6;
    var maxLeng = 4;
    var letterArrayIndex = 0;

    var letterArray = [" Thousand", " Million", " Billion", " Trillion", " Quadrillion", " Quintillion", " Sextillion", " Septillion", " Octillion", " Nonillion", " Decillion", " Undecillion", " Duodecillion", " Tredecillion", " Quatuordecillion", " Quindecillion", " Sexdecillion", " Septendecillion", " Octodecillion", " Novemdecillion", " Vigintillion", " Unvigintillion", " Duovigintillion", " Tresvigintillion", " Quatuorvigintillion", " Quinquavigintillion", " Sesvigintillion", " Septemvigintillion", " Octovigintillion", " Novemvigintillion", " Trigintillion", " Untrigintillion", " Duotrigintillion", " Trestrigintillion", " Quatuortrigintillion", " Quinquatrigintillion", " Sestrigintillion", " Septentrigintillion", " Octotrigintillion", " Novemtrigintillion", " Quadragintillion", " Unquadragintillion", " Duoquadragintillion", " Tresquadragintillion", " Quatuorquadragintillion", " Quinquaquadragintillion", " Sesquadragintillion", " Septemquadragintillion", " Octoquadragintillion", " Novemquadragintillion", " Quinquagintillion", " Unquinquagintillion", " Duoquinquagintillion", " Tresquinquagintillion", " Quatuorquinquagintillion", " Quinquaquinquagintillion", " Sesquinquagintillion", " Septenquinquagintillion", " Octoquinquagintillion", " Novemquinquagintillion", " Sexagintillion", " Unsexagintillion", " Duosexagintillion", " Tressexagintillion", " Quatuorsexagintillion", " Quinquasexagintillion", " Sexasexagintillion", " Septemsexagintillion", " Octosexagintillion", " Novemsexagintillion", " Septuagintillion", " Unseptuagintillion", " Duoseptuagintillion", " Tresseptuagintillion", " Quatuorseptuagintillion", " Quinquaseptuagintillion", " Sexaseptuagintillion", " Septenseptuagintillion", " Octoseptuagintillion", " Novemseptuagintillion", " Octogintillion", " Unoctogintillion", " Duooctogintillion", " Tresoctogintillion", " Quatuoroctogintillion", " Quinquaoctogintillion", " Sesoctogintillion", " Septemoctogintillion", " Octooctogintillion", " Novemoctogintillion", " Nonagintillion", " Unnonagintillion", " Duononagintillion", " Tresnonagintillion", " Quatuornonagintillion", " Quinquanonagintillion", " Sesnonagintillion", " Septemnonagintillion", " Octononagintillion", " Novemnonagintillion", " Centillion", " Uncentillion"];

    var leng = 4;
    var slic = 1;

    for (var g = 0; g < visualOfIt.length; g++) {
        if (visualOfIt.length <= visualLeng) {
            if (leng < maxLeng) {
                leng = maxLeng;
            }

            if (visualOfIt.length === leng) {
                if (slic > 2) {
                    visualOfIt = costOfIt.toString().slice(0, slic) + letterArray[letterArrayIndex];
                    break;
                } else {
                    visualOfIt = costOfIt.toString().slice(0, slic) + "," + costOfIt.toString().slice(slic, 3) + letterArray[letterArrayIndex];
                    break;
                }
            } else {
                leng++;
                slic++;
            }
        } else {
            maxLeng += 3;
            visualLeng += 3;
            letterArrayIndex++;
        }
    }

    return visualOfIt;
}

Just use console.log(simpleNumber(1435345, 0)) outside of the function then it would return 1,43 Million

Upvotes: 0

Related Questions