I want to convert number into words. Like 234 in word would be 2 hundred thirty-four. Similarly 1000 will be one thousand. Similarly 100 means one hundred.
Is there any library for this in angularjs. If then how we will write in angularjs directive/service so that we can use it again..
Create a words filter.
I got the "toWords" algorithm from this blog post: http://ravindersinghdang.blogspot.com/2013/04/convert-numbers-into-words-using.html
var app = angular.module('app',[]);
app.filter('words', function() {
function isInteger(x) {
return x % 1 === 0;
}
return function(value) {
if (value && isInteger(value))
return toWords(value);
return value;
};
});
var th = ['','thousand','million', 'billion','trillion'];
var dg = ['zero','one','two','three','four', 'five','six','seven','eight','nine'];
var tn = ['ten','eleven','twelve','thirteen', 'fourteen','fifteen','sixteen', 'seventeen','eighteen','nineteen'];
var tw = ['twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety'];
function toWords(s)
{
s = s.toString();
s = s.replace(/[\, ]/g,'');
if (s != parseFloat(s)) return 'not a number';
var x = s.indexOf('.');
if (x == -1) x = s.length;
if (x > 15) return 'too big';
var n = s.split('');
var str = '';
var sk = 0;
for (var i=0; i < x; i++)
{
if ((x-i)%3==2)
{
if (n[i] == '1')
{
str += tn[Number(n[i+1])] + ' ';
i++;
sk=1;
}
else if (n[i]!=0)
{
str += tw[n[i]-2] + ' ';
sk=1;
}
}
else if (n[i]!=0)
{
str += dg[n[i]] +' ';
if ((x-i)%3==0) str += 'hundred ';
sk=1;
}
if ((x-i)%3==1)
{
if (sk) str += th[(x-i-1)/3] + ' ';
sk=0;
}
}
if (x != s.length)
{
var y = s.length;
str += 'point ';
for (var i=x+1; i<y; i++) str += dg[n[i]] +' ';
}
return str.replace(/\s+/g,' ');
}
window.toWords = toWords;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.js"></script>
<div ng-app="app">
<input type="text" ng-model="name" /> {{name | words}}
</div>
I have done this for Angular 8.
It also works for float/double numbers.
Related
I need to convert amount in words. For example, the amount I will get it from service is 9876, I need to display in a table "Nine Thousand Eight Hundred and Seventy Six" in a table.
I need to do this using angularjs. Please help me how can I do this.
JSFIDDLE
function convertNumberToWords(amount) {
var words = new Array();
words[0] = '';
words[1] = 'One';
words[2] = 'Two';
words[3] = 'Three';
words[4] = 'Four';
words[5] = 'Five';
words[6] = 'Six';
words[7] = 'Seven';
words[8] = 'Eight';
words[9] = 'Nine';
words[10] = 'Ten';
words[11] = 'Eleven';
words[12] = 'Twelve';
words[13] = 'Thirteen';
words[14] = 'Fourteen';
words[15] = 'Fifteen';
words[16] = 'Sixteen';
words[17] = 'Seventeen';
words[18] = 'Eighteen';
words[19] = 'Nineteen';
words[20] = 'Twenty';
words[30] = 'Thirty';
words[40] = 'Forty';
words[50] = 'Fifty';
words[60] = 'Sixty';
words[70] = 'Seventy';
words[80] = 'Eighty';
words[90] = 'Ninety';
amount = amount.toString();
var atemp = amount.split(".");
var number = atemp[0].split(",").join("");
var n_length = number.length;
var words_string = "";
if (n_length <= 9) {
var n_array = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0);
var received_n_array = new Array();
for (var i = 0; i < n_length; i++) {
received_n_array[i] = number.substr(i, 1);
}
for (var i = 9 - n_length, j = 0; i < 9; i++, j++) {
n_array[i] = received_n_array[j];
}
for (var i = 0, j = 1; i < 9; i++, j++) {
if (i == 0 || i == 2 || i == 4 || i == 7) {
if (n_array[i] == 1) {
n_array[j] = 10 + parseInt(n_array[j]);
n_array[i] = 0;
}
}
}
value = "";
for (var i = 0; i < 9; i++) {
if (i == 0 || i == 2 || i == 4 || i == 7) {
value = n_array[i] * 10;
} else {
value = n_array[i];
}
if (value != 0) {
words_string += words[value] + " ";
}
if ((i == 1 && value != 0) || (i == 0 && value != 0 && n_array[i + 1] == 0)) {
words_string += "Crores ";
}
if ((i == 3 && value != 0) || (i == 2 && value != 0 && n_array[i + 1] == 0)) {
words_string += "Lakhs ";
}
if ((i == 5 && value != 0) || (i == 4 && value != 0 && n_array[i + 1] == 0)) {
words_string += "Thousand ";
}
if (i == 6 && value != 0 && (n_array[i + 1] != 0 && n_array[i + 2] != 0)) {
words_string += "Hundred and ";
} else if (i == 6 && value != 0) {
words_string += "Hundred ";
}
}
words_string = words_string.split(" ").join(" ");
}
return words_string;
}
<input type="text" name="number" placeholder="Number OR Amount" onkeyup="word.innerHTML=convertNumberToWords(this.value)" />
<div id="word"></div>
I refered this javascript fiddle. But I want to it in a angularjs.
I used this for Angular 8.
Input : 123456789.09 Output : twelve crore thirty four lakh fifty six thousand seven eighty nine point zero nine
n: string;
a = ['zero ', 'one ', 'two ', 'three ', 'four ', 'five ', 'six ', 'seven ', 'eight ', 'nine ', 'ten ', 'eleven ', 'twelve ', 'thirteen ', 'fourteen ', 'fifteen ', 'sixteen ', 'seventeen ', 'eighteen ', 'nineteen '];
b = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
ngOnInit(): void {
console.log(this.inWords(123456789.09));
}
inWords (num): string {
num = Math.floor(num * 100);
if ((num = num.toString()).length > 11) { return 'overflow'; }
let n;
n = ('00000000' + num).substr(-11).match(/^(\d{2})(\d{2})(\d{2})(\d{1})(\d{2})(\d{1})(\d{1})$/);
if (!n) { return; } let str = '';
// tslint:disable-next-line:triple-equals
str += (n[1] != 0) ? (this.a[Number(n[1])] || this.b[n[1][0]] + ' ' + this.a[n[1][1]]) + 'crore ' : '';
// tslint:disable-next-line:triple-equals
str += (n[2] != 0) ? (this.a[Number(n[2])] || this.b[n[2][0]] + ' ' + this.a[n[2][1]]) + 'lakh ' : '';
// tslint:disable-next-line:triple-equals
str += (n[3] != 0) ? (this.a[Number(n[3])] || this.b[n[3][0]] + ' ' + this.a[n[3][1]]) + 'thousand ' : '';
// tslint:disable-next-line:triple-equals
str += (n[4] != 0) ? (this.a[Number(n[4])] || this.b[n[4][0]] + ' ' + this.a[n[4][1]]) : 'hundred';
// tslint:disable-next-line:triple-equals
str += (n[5]) ? (this.a[Number(n[5])] || this.b[n[5][0]] + ' ' + this.a[n[5][1]]) : '';
// tslint:disable-next-line:triple-equals
str += (n[6]) ? ((str != '') ? 'point ' : '') + (this.a[Number(n[6])] || this.b[n[6][0]] + ' ' + this.a[n[6][1]]) : '';
// tslint:disable-next-line:triple-equals
str += (n[7] != 0) ? (this.a[Number(n[7])] || this.b[n[7][0]] + ' ' + this.a[n[7][1]]) : '';
return str;
}
Define a filter to convert number to word such as following code:
angular.module('myModuleName')
.filter('convertToWord', function() {
return function(amount) {
var words = new Array();
words[0] = '';
words[1] = 'One';
words[2] = 'Two';
words[3] = 'Three';
words[4] = 'Four';
words[5] = 'Five';
words[6] = 'Six';
words[7] = 'Seven';
words[8] = 'Eight';
words[9] = 'Nine';
words[10] = 'Ten';
words[11] = 'Eleven';
words[12] = 'Twelve';
words[13] = 'Thirteen';
words[14] = 'Fourteen';
words[15] = 'Fifteen';
words[16] = 'Sixteen';
words[17] = 'Seventeen';
words[18] = 'Eighteen';
words[19] = 'Nineteen';
words[20] = 'Twenty';
words[30] = 'Thirty';
words[40] = 'Forty';
words[50] = 'Fifty';
words[60] = 'Sixty';
words[70] = 'Seventy';
words[80] = 'Eighty';
words[90] = 'Ninety';
amount = amount.toString();
var atemp = amount.split(".");
var number = atemp[0].split(",").join("");
var n_length = number.length;
var words_string = "";
if (n_length <= 9) {
var n_array = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0);
var received_n_array = new Array();
for (var i = 0; i < n_length; i++) {
received_n_array[i] = number.substr(i, 1);
}
for (var i = 9 - n_length, j = 0; i < 9; i++, j++) {
n_array[i] = received_n_array[j];
}
for (var i = 0, j = 1; i < 9; i++, j++) {
if (i == 0 || i == 2 || i == 4 || i == 7) {
if (n_array[i] == 1) {
n_array[j] = 10 + parseInt(n_array[j]);
n_array[i] = 0;
}
}
}
value = "";
for (var i = 0; i < 9; i++) {
if (i == 0 || i == 2 || i == 4 || i == 7) {
value = n_array[i] * 10;
} else {
value = n_array[i];
}
if (value != 0) {
words_string += words[value] + " ";
}
if ((i == 1 && value != 0) || (i == 0 && value != 0 && n_array[i + 1] == 0)) {
words_string += "Crores ";
}
if ((i == 3 && value != 0) || (i == 2 && value != 0 && n_array[i + 1] == 0)) {
words_string += "Lakhs ";
}
if ((i == 5 && value != 0) || (i == 4 && value != 0 && n_array[i + 1] == 0)) {
words_string += "Thousand ";
}
if (i == 6 && value != 0 && (n_array[i + 1] != 0 && n_array[i + 2] != 0)) {
words_string += "Hundred and ";
} else if (i == 6 && value != 0) {
words_string += "Hundred ";
}
}
words_string = words_string.split(" ").join(" ");
}
return words_string;
};
});
Then in your templates (views) use this filter as follow:
{{amount | converToWord}}
For example to show inserted value in an input field:
<input type="text" name="number" placeholder="Number OR Amount" ng-model="myValue" />
<div id="word">{{myValue | convertToWord}}</div>
I have a function that involves declaring and populating an array called ArrayRoute. I need to reference this array later in a for loop. How do I set this array as global so I can access it outside of the function?
Thanks
function route(permutation, origins) {
var myroute = origins[0];
var ArrayRoute = [];
ArrayRoute.push(origins[0]);
console.log('ArrayRoute= ' + ArrayRoute);
for (var i = 0; i < permutation.length; i++){
myr += '\n' + myd[permutation[i] - 1];
ArrayRoute.push(myd[permutation[i] - 1]);
}
return myroute;
}
for (i = 0; i < ArrayRoute.length - 1; i++) {
console.log(ArrayRoute[i] + '->' + ArrayRoute[i + 1]);
}
console.log('ArrayRouteeee= ' + ArrayRoute);
You can't.
If you create the array inside the function then it is only accessible from within the function body.
You can declare it outside of the function:
let ArrayRoute = [];
function route(permutation, origins) {
var myroute = origins[0];
ArrayRoute.push(origins[0]);
console.log('ArrayRoute= ' + ArrayRoute);
for (var i = 0; i < permutation.length; i++){
myr += '\n' + myd[permutation[i] - 1];
ArrayRoute.push(myd[permutation[i] - 1]);
}
return myroute;
}
for (i = 0; i < ArrayRoute.length - 1; i++) {
console.log(ArrayRoute[i] + '->' + ArrayRoute[i + 1]);
}
console.log('ArrayRouteeee= ' + ArrayRoute);
You can pass it as a parameter to the function:
function route(permutation, origins, ArrayRoute) {
...
}
Or you can return the array as a result from the function:
function route(permutation, origins) {
var myroute = origins[0];
var ArrayRoute = [];
ArrayRoute.push(origins[0]);
console.log('ArrayRoute= ' + ArrayRoute);
for (var i = 0; i < permutation.length; i++){
myr += '\n' + myd[permutation[i] - 1];
ArrayRoute.push(myd[permutation[i] - 1]);
}
return { myroute, ArrayRoute };
}
let { myroute, ArrayRoute } = route(...);
Everything pre-loop is fine. I need help finding the way to access deeper level json. I'm stuck in the "for" loop. The product_name comes out okay in the first loop but nothing deeper than that. I've added some output for each deeper level loop but I don't seem to pass the second one.
Follow URL to view JSON array.
<script>
var xmlhttp = new XMLHttpRequest();
var url = 'http://www.weber.se/?type=88&pageAlias=lecareglttklinker26&json=1';
xmlhttp.onreadystatechange=function() {
if (this.readyState == 4 && this.status == 200) {
readJson(this.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
function readJson(response) {
var object = JSON.parse(response);
var output = '<div class="container-fluid">';
output += '<div class="row">';
output += '<div class="col-xs-6 text-left"><img src="' + object.product[0].packaging_picture + '" height="230"></div>';
output += '<div class="col-xs-6 text-right"><img src="' + object.product[0].main_picture + '" height="230"></div>';
output += '</div>';
output += '<div class="row">';
output += '<div>' + object.product[0].standfirst + '</div>';
output += '</div>';
output += '<div class="row">';
output += '<div class="well"><h2>Egenskaper</h2>' + object.product[0].benefits_description + '</div>';
output += '</div>';
for (var a = 0; a < object.product.length; a++) {
var product = object.product[a];
var name = product.product_name;
output += '<h2>' + name + '</h2>';
for (var b = 0; b < product.tabs.length; b++) {
var tabs = product.tabs[b];
output += 'tabs<br>';
for (var c = 0; c < product.tabs.tab.length; c++) {
var tab = tabs.tab[c];
output += 'tab<br>';
for (var d = 0; d < product.tabs.tab.contents.length; d++) {
var contents = tab.contents[d];
output += 'contents<br>';
for (var e = 0; e < product.tabs.tab.contents.content.length; e++) {
var content = contents.content[e];
output += 'content<br>';
for (var f = 0; f < product.tabs.tab.contents.content.title.length; f++) {
var title = content.title[f];
var bodytext = content.bodytext[f];
output += '<h3>' + title + '</h3>';
output += bodytext;
}
}
}
}
}
}
output += '</div>';
document.getElementById("output").innerHTML = output;
}
</script>
Your second loop is not valid. as per the data in the link, product.tabs is not an array. its an object. so you can't really loop and object you need to have a different way to process that object.
for (var a = 0; a < object.product.length; a++) {
var product = object.product[a];
var name = product.product_name;
output += '<h2>' + name + '</h2>';
output += 'tabs<br>';
var tabs = product['tabs']['tab'];
for (var c = 0; c < tabs.length; c++) {
var tab = tabs[c];
output += 'tab<br>';
var contents = tab['contents'] != undefined ? tab['contents']['content'] : [];
output += 'contents<br>';
for (var e = 0; e < contents.length; e++) {
var content = contents[e];
output += 'content<br>';
var title = content.title;
var bodytext = content.bodytext;
output += '<h3>' + title + '</h3>';
output += bodytext;
}
}
}
I have the following snippet of code that creates 1, 2, 3 etc based on the number in parent repeat (All good).
{{$index+1}}
But I am looking for Text such as One, Two Three etc
Is there a way to achieve this in Angular? (Text instead of numerics)
One solution is to create a filter, for example:
// Script.js
angular.module('app')
.filter('numberToWord', function() {
return function( s ) {
var th = ['','thousand','million', 'billion','trillion'];
var dg = ['zero','one','two','three','four', 'five','six','seven','eight','nine'];
var tn = ['ten','eleven','twelve','thirteen', 'fourteen','fifteen','sixteen', 'seventeen','eighteen','nineteen'];
var tw = ['twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety'];
s = s.toString();
s = s.replace(/[\, ]/g,'');
if (s != parseFloat(s)) return 'not a number';
var x = s.indexOf('.');
if (x == -1) x = s.length;
if (x > 15) return 'too big';
var n = s.split('');
var str = '';
var sk = 0;
for (var i=0; i < x; i++)
{
if ((x-i)%3==2)
{
if (n[i] == '1')
{
str += tn[Number(n[i+1])] + ' ';
i++;
sk=1;
}
else if (n[i]!=0)
{
str += tw[n[i]-2] + ' ';
sk=1;
}
}
else if (n[i]!=0)
{
str += dg[n[i]] +' ';
if ((x-i)%3==0) str += 'hundred ';
sk=1;
}
if ((x-i)%3==1)
{
if (sk) str += th[(x-i-1)/3] + ' ';
sk=0;
}
}
if (x != s.length)
{
var y = s.length;
str += 'point ';
for (var i=x+1; i<y; i++) str += dg[n[i]] +' ';
}
return str.replace(/\s+/g,' ');
};
})
And then you can use it like this:
{{$index | numberToWord }}
Edit: An example Plunker http://plnkr.co/edit/DIQ3YVkH7N6PUaqju82X?p=preview
There is no standard feature in Angular for that. Here is a link to a great algorithm to convert number to word Developers blog - Convert Numbers into Words Using JavaScript
Put the algorithm in a function and use it like this :
{{convertNumberToWord($index+1)}}
Here is a working example in Plunker for your situation
I have created an Array in frame 1 and dynamically create a list of movieclips with it , and I want to create the same Array in frame 3 and those movieclips again if something is true or false.is it possible ? because when I'm trying to do this , I will get this error :
1151: A conflict exists with definition i in namespace internal.
here is my code at frame 1 :
stop();
import flash.display.MovieClip;
var p_x:uint = 90;
var p_y:uint = 108;
var my_list:String = "Games,Videos, Medias,Images,Photos,Personal Photos,Social Media,Private,Social,None,Names,Families";
var myListString:Array = my_list.split(",");
var myArray:Array=new Array ();
var listObject = 1;
for (var i:uint=0; i<12; i++)
{
var myItemList:mrb=new mrb();
myItemList.x = p_x;
myItemList.y = p_y + 80;
myItemList.y = (p_y + (listObject * 65));
myArray.push(myItemList);
myItemList.txt.text = i.toString();
myItemList.txt.text = myListString[i].toString();
myItemList.name = "item"+i;
addChild(myItemList) as MovieClip ;
listObject++;
}
and here is code at frame 3 :
var tmpCurFrame:int = currentFrame;
this.addEventListener(Event.ENTER_FRAME, handleUpdate)
function handleUpdate(e:Event):void {
if (tmpCurFrame != currentFrame) {
this.removeEventListener(Event.ENTER_FRAME, handleUpdate);
return;
}
if (so.data.fav1 != undefined)
{
if ( so.data.fav1 == "on")
{
for (var i:int = 0; i < myListString.length;)
{ if (myListString[i].indexOf() > -1)
{
var myRectangle:mrb=new mrb();
trace("found it at index: " + i);
myRectangle.x = p_x;
myRectangle.y = (p_y + (findobject * 50));
trace('p_y+80 =' + (p_y+(findobject*80)) + 'p_x = ' + p_x );
myArray.push(myRectangle);
myRectangle.txt.text = myListString[i].toString();
trace(my_array2[i].toString() );
addChild(myRectangle);
}
}
}
}
else
{
fav_1.fav_on.visible=true;
}
}
This error message simply means that you use twice the same variable i. You just have to give them differents names.