30

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);
MahdiY
  • 1,269
  • 21
  • 32
JAVA
  • 305
  • 1
  • 4
  • 8
  • 1
    Please give some examples of inputs and expected results for the function – Steven Manuel Apr 20 '16 at 05:14
  • i want display like 6.8M for all values. – JAVA Apr 20 '16 at 05:32
  • "for some numbers it is not converting" what are examples of it not converting? – nonopolarity Apr 20 '16 at 05:46
  • you mean... even for `600000`, you want it to show as `0.6M`? – nonopolarity Apr 20 '16 at 05:58
  • no..the values can be in thousands,lakhs or etc..but when we convert into million for all values, it should come as 6.8M – JAVA Apr 20 '16 at 06:24
  • I do see it working fine except negative numbers. What numbers do you see not working? – nonopolarity Apr 20 '16 at 06:59
  • for some of numbers its not converting...like the original value is '2086396',but after converting it is showing like '2.086396M' – JAVA Apr 20 '16 at 07:16
  • You need to include as much information in your question in order for others to help you better. "Some numbers not converting" is not good enough. What are the numbers, what output are you expecting, etc. which you eventually provided in the comments. These should have been included in the question. – Steven Manuel Apr 21 '16 at 01:16
  • i don't think i have enough for a full answer, but for anyone else landing here, it might be worth looking into the Intl number formatter. It includes short and long suffixes (K or thousand; M or million, B or billions, etc): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options – worc Sep 13 '22 at 05:54

7 Answers7

84

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/

Adil Malik
  • 6,279
  • 7
  • 48
  • 77
Steven Manuel
  • 1,898
  • 1
  • 18
  • 22
5

I know I'm almost 16 years late at the party, but I can't help myself to comment what I know about it after seeing so much long functions for this task. even the accepted and most upvoted answer uses a quite long function

However, 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()

Pratik Dev
  • 304
  • 2
  • 8
2

getNumber = function(num) {
    
    var units = ["M","B","T","Q"]
    var unit = Math.floor((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 x.toFixed(2)+ ' ' + units[Math.floor(unit / 3) - 2]
}

console.log(getNumber(680000))
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))
Balaji
  • 9,657
  • 5
  • 47
  • 47
Vikram Biwal
  • 2,618
  • 1
  • 25
  • 36
1

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)); 
Balaji
  • 9,657
  • 5
  • 47
  • 47
Jeff Weinberg
  • 414
  • 4
  • 9
  • The last line needs to multiply by sign as well or you'll make small negative numbers positive. `sign * Math.abs(Number(labelValue));` or just `labelValue` – Jimmy_barnes Jun 06 '21 at 14:21
1

    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

Balaji
  • 9,657
  • 5
  • 47
  • 47
orr burgel
  • 381
  • 1
  • 6
  • 16
0

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

MartenCatcher
  • 2,713
  • 8
  • 26
  • 39
Minenik
  • 1
  • 1
  • 1
0

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
Meddie
  • 571
  • 1
  • 7
  • 22