javascript - 将数值(金额)转换为文本 angularjs

标签 javascript angularjs asp.net-mvc-5 numbers

我正在使用 angularJS 在 mvc-5 中创建一个基于 Web 的应用程序,我在表中得到了金额总和

<td><span style="font-weight:bolder; font-size:24px; float:right;">Total: {{totalAmount}}</span></td>

我像这样从 Controller 获取金额

$scope.totalAmount = 0;
$scope.tableindiv2.forEach(function (t) {
   $scope.totalAmount += Number(t.amount);
   $scope.empnameandaddress();
});

这里我想将金额从数字转换为文本。例如,如果总数为 4526,则应显示 四千五百二十六

最佳答案

您可以将数字转换为单词,就像 this 中所做的那样回答。

var a = ['','one ','two ','three ','four ', 'five ','six ','seven ','eight ','nine ','ten ','eleven ','twelve ','thirteen ','fourteen ','fifteen ','sixteen ','seventeen ','eighteen ','nineteen '];
var b = ['', '', 'twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety'];

function inWords (num) {
    if ((num = num.toString()).length > 9) return 'overflow';
    n = ('000000000' + num).substr(-9).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})$/);
    if (!n) return; var str = '';
    str += (n[1] != 0) ? (a[Number(n[1])] || b[n[1][0]] + ' ' + a[n[1][1]]) + 'crore ' : '';
    str += (n[2] != 0) ? (a[Number(n[2])] || b[n[2][0]] + ' ' + a[n[2][1]]) + 'lakh ' : '';
    str += (n[3] != 0) ? (a[Number(n[3])] || b[n[3][0]] + ' ' + a[n[3][1]]) + 'thousand ' : '';
    str += (n[4] != 0) ? (a[Number(n[4])] || b[n[4][0]] + ' ' + a[n[4][1]]) + 'hundred ' : '';
    str += (n[5] != 0) ? ((str != '') ? 'and ' : '') + (a[Number(n[5])] || b[n[5][0]] + ' ' + a[n[5][1]]) + 'only ' : '';
    return str;
}

$scope.totalAmount = 0;
$scope.tableindiv2.forEach(function (t) {
   $scope.totalAmount += Number(t.amount);
   $scope.empnameandaddress();
});

$scope.totalAmountInWords = inWords($scope.totalAmount);

在 HTML View 中,使用 totalAmountInWords 而不是 totalAmount

style="font-weight:bolder; font-size:24px; float:right;">Total: {{totalAmountInWords }}</span></td>

关于javascript - 将数值(金额)转换为文本 angularjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41821226/

相关文章:

AngularJs 拦截器的循环依赖

javascript - Uncaught Error bootstrap 需要在 bootstrap.min.js 处使用 jquery

AngularJS获取passportjs策略失败消息

mobile - 在 ASP.NET MVC 5 应用程序中通过摄像头读取条码

c# - ASP.NET MVC 处理区域中的请求验证异常

javascript - 在javascript中将十进制打印为二进制

javascript - 20 13 和侧边栏溢出页脚 - 顶部边距

javascript - react native 导航实验和 MapView 手势

asp.net-mvc-5 - XsrfKey 的用途是什么?我应该将 XsrfId 设置为其他内容吗?

javascript - 快速 HTML 表格排序?