如何在javascript中将数字转换为百万 [英] how to convert numbers to million in javascript
问题描述
如图中所示.对于某些值可以正确转换但某些值不能转换...您可以在图像中看到
As in image. for some values converting correctly but some of values not converting... you can see in image
我想将数字转换为百万.我正在使用Money格式函数转换数字,但是我无法转换数字.
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));
}
在这里,我正在使用Moneyformat转换数字.这是我要转换数字的控制器部分
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);
推荐答案
我不知道$ scope.MoneyFormat是什么.
I have no idea what $scope.MoneyFormat is.
所以我将您的函数简化为普通的js函数,并且可以正常工作.
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/
JSFiddle: https://jsfiddle.net/r5ju34ey/
这篇关于如何在javascript中将数字转换为百万的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!