如何在javascript中将数字转换为百万

时间:2016-04-20 05:00:21

标签: javascript

如同图像一样。对于某些值正确转换但有些值未转换...您可以在图像中看到

我想将数字转换为百万。我使用Money格式转换数字但我无法转换数字。

这是控制器部分。对于某些数字,它转换为数百万,而对于某些数字,它没有转换..请有人帮忙。

 $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));
                   }

我在这里使用Moneyformat转换数字。这是我转换数字的控制器部分

            $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);

5 个答案:

答案 0 :(得分:26)

我不知道$ scope.MoneyFormat是什么。

所以我将你的函数简化为一个普通的旧js函数,它可以工作。

function test (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));

}

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

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

答案 1 :(得分:2)

getNumberUnit = function(num) {
    var units = ["Million","Billion","Trillion","Quadrillion","Quintillion","Sextillion"]
    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]
}

最好的

答案 2 :(得分:0)

由于javascript编号走了多远,所以它们最多只能达到1e301 此功能显示3位数字和一个字母,例如public static function indexQuery(NovaRequest $request, $query) { return $query->when(! $request->user()->is_super_admin, function ($builder) use ($request) { // Or use the request() method helper instead of use return $request->user()->with('mailboxes'); }); } 将是3000000000000000030,0 Quadrillion将是1000000

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; } ,它将返回console.log(simpleNumber(1435345, 0))

答案 3 :(得分:0)

改进答案以包含否定词:

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)); 

答案 4 :(得分:0)

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))