Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert numbers to million in javascript

Tags:

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); 
like image 765
JAVA Avatar asked Apr 20 '16 05:04

JAVA


People also ask

How do you format numbers in JavaScript?

JavaScript numbers can be formatted in different ways like commas, currency, etc. You can use the toFixed() method to format the number with decimal points, and the toLocaleString() method to format the number with commas and Intl. NumberFormat() method to format the number with currency.

How do you convert a number to an integer in JavaScript?

In JavaScript parseInt() function (or a method) is used to convert the passed in string parameter or value to an integer value itself. This function returns an integer of base which is specified in second argument of parseInt() function.


2 Answers

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/

like image 67
Steven Manuel Avatar answered Oct 05 '22 22:10

Steven Manuel


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));  
like image 31
Jeff Weinberg Avatar answered Oct 05 '22 21:10

Jeff Weinberg