1

I am creating a web based application in mvc-5 using angularJS i am getting a sum of amount in my table

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

I am getting the amount from the controller like this

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

Here i want to convert the amount from numbers to text. For example, if the total is 4526, it should show four thousand five hundred twenty six.

CozyAzure
  • 8,280
  • 7
  • 34
  • 52

3 Answers3

1

You can convert from number to words like done in this answer.

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

In your HTML view, use totalAmountInWords instead of totalAmount.

style="font-weight:bolder; font-size:24px; float:right;">Total: {{totalAmountInWords }}</span></td>
Community
  • 1
  • 1
Ravi Shankar Bharti
  • 8,922
  • 5
  • 28
  • 52
0

You have to create word filter which convert number to text

Please refer this Angular directive/service to convert number into words (need in Angularjs)?

Community
  • 1
  • 1
Manoj Patidar
  • 1,171
  • 2
  • 11
  • 29
0

I have done this for Angular 8. It can also convert float/double numbers into word.

Progga Ilma
  • 578
  • 6
  • 8