gpt4 book ai didi

javascript - 以 Angular 格式将数字格式转换为货币格式

转载 作者:行者123 更新时间:2023-11-30 08:33:33 28 4
gpt4 key购买 nike

我想将数字转换为数字格式。

到目前为止我已经试过了:http://plnkr.co/edit/lVJGXyuX0BMvB9QUL5zS?p=preview

function formatMoney(credits) {

console.log(credits+'credits');
var lastThree = credits.substring(credits.length-3);
// var lastThree = credits.slice(-2);
var otherNumbers = credits.substring(0,credits.length-3);
console.log(otherNumbers+'otherNumbers');
if(otherNumbers !== '')
lastThree = ',' + lastThree;
var res = otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + lastThree;
return res;
}

function formatNumber(num) {
var n1, n2;
num = (Math.round(num * 100) / 100) + '' || '';
n1 = num.split('.');
n2 = n1[1] || null;
n1 = n1[0].replace(/(\d)(?=(\d\d)+\d$)/g, ",");
num = n2 ? n1 + '.' + n2 : n1;
return num;
}

我想将数字转换为印度货币。

示例:

1,000
10,000
1,00,000
10,00,000

我使用函数 formatMoney() 获得的输出:1,0,0,000

我使用函数 formatNumber() 获得的输出:1,000,在按 0 后,它变为 NaN0

我在这里做错了什么?

最佳答案

AngularJS 提供者过滤器做这些事情,有一个 currency filter可用:

我已经重写了您的代码,删除了格式化功能并替换为货币过滤器和印度货币的卢比符号,希​​望这能解决您的问题。

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {

$scope.change=function($event, money){
$scope.money = qwerty;
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html ng-app="plunker">

<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.2.x" src="https://code.angularjs.org/1.2.20/angular.js" data-semver="1.2.20"></script>
<script src="app.js"></script>
</head>

<body ng-controller="MainCtrl">
<input ng-keypress="change($event, money)" type="text" ng-model='money' >
<pre>{{money | currency : "&#8377;" }}</pre>
</body>

</html>

关于javascript - 以 Angular 格式将数字格式转换为货币格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34472381/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com