-2

I have a string that is exactly three numbers, for example; 999. I want to replace the last two 9's with the words ninety-nine. I have looked searched around a lot, but for some reason I can't get anything to work. I also want to know how I would replace the first 9 with the words "nine hundred", but I don't want to have them done at the same time. It would be helpful if you could explain how it works, too, because I am relatively new to programming.

Thanks

CPC
  • 163
  • 5
  • 17
  • http://stackoverflow.com/questions/5529934/javascript-numbers-to-words – nzn Dec 28 '13 at 22:30
  • @nzn I think that's far more advanced that what he's looking for. I think this is a substring level thing. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr – Robert Dec 28 '13 at 22:32
  • I'm new to programming like I said before, so I don't exactly get what is happening there. Could you explain it more simply? You don't have to worry about the HTML part or putting it in a function, but I just want a simple answer to the question. Thanks – CPC Dec 28 '13 at 22:34
  • So, you want us to write a program that changes Numbers to English values? I'm sure you can find something online, without us writing it for you. If not use `String.length` and `String.replace()`. – StackSlave Dec 28 '13 at 22:37
  • 1
    **Voting to close for being off-topic**: _"Questions asking for code must demonstrate a minimal understanding of the problem being solved. **Include attempted solutions, why they didn't work**, and the expected results. See also: [Stack Overflow question checklist](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist)."_ – Sparky Dec 28 '13 at 23:03

4 Answers4

0
yourString = "999";

for (var x = 3; x > 1; x--) {
    yourString[x] = "nine hundred";
    x -= 1;
}
CRABOLO
  • 8,605
  • 39
  • 41
  • 68
0

You can easily replace the last two numbers at the end of a string with a regular expression:

var value = "999";

value = value.replace(/\d{2}$/, "ninety-nine"); // 9ninety-nine

Note that this doesn't actually know that "99" is "ninety-nine". Neither does it know that "39" is "thirty-nine". That task is far more difficult to achieve than merely replacing parts of a string.

Hope this helps.

Sampson
  • 265,109
  • 74
  • 539
  • 565
  • I replaced the "999" at the beginning with the name of my variable and tried alerting the variable, but it still says "999"; – CPC Dec 28 '13 at 22:42
  • You would have to re-assign the resulting value to your original variable. – Sampson Dec 28 '13 at 22:42
  • Huh? I don't get what you mean. – CPC Dec 28 '13 at 22:43
  • And I was able to figure out that it doesn't know that "99" is the same as "ninety-nine". Also, what would you change so that it replaces the first character with something else for a different part? – CPC Dec 28 '13 at 22:45
  • @user3130551 What would you want to replace the first character with? – Sampson Dec 29 '13 at 02:21
0

I suppose you need all the numbers to be converted to english words. Not just 999. 999 was just an example.

You can do this with multiple switch.

Something like that:

You can see and the demo

var str="999";
var result="";

var first =str.charAt(0);//var first="9" in your example
switch (first){
case "9": 
result=result+"nine hundred "
break;
case "8": 
result=result+"eight hundred "
break;
case "7": 
result=result+"seven hundred "
break;
case "6": 
result=result+"six hundred "
break;
case "5": 
result=result+"five hundred "
break;
case "4": 
result=result+"four hundred "
break;
case "3": 
result=result+"three hundred "
break;
case "2": 
result=result+"two hundred "
break;
case "1": 
result=result+"one hundred "
break;        
default : result=result+"NaN"
}

var second =str.charAt(1);//var second="9" in your example
switch (second){
case "9": 
result=result+"ninety "
break;
case "8": 
result=result+"eighty "
break;
case "7": 
result=result+"seventy "
break;
case "6": 
result=result+"sixty "
break;
case "5": 
result=result+"fifty "
break;
case "4": 
result=result+"fourty "
break;
case "3": 
result=result+"thirty "
break;
case "2": 
result=result+"twenty "
break;
case "1": 
result=result+"ten "
break;        
default : result=result+"NaN"
}

var third =str.charAt(2); //var third="9" in your example
switch (third){
case "9": 
result=result+"nine"
break;
case "8": 
result=result+"eight"
break;
case "7": 
result=result+"seven"
break;
case "6": 
result=result+"six"
break;
case "5": 
result=result+"five"
break;
case "4": 
result=result+"four"
break;
case "3": 
result=result+"three"
break;
case "2": 
result=result+"two"
break;
case "1": 
result=result+"one"
break;        
default : result=result+"NaN"
}
alert(result);
laaposto
  • 11,835
  • 15
  • 54
  • 71
  • what is the "var first =str.charAt(0);" doing in that? – CPC Dec 28 '13 at 22:46
  • it assignes to `var first` the first character of the sting-number you have. In you example 999 `var first="9"` – laaposto Dec 28 '13 at 22:49
  • I have an array called values. How do I apply that to every object in the array? – CPC Dec 28 '13 at 22:59
  • You will have to make a `for-loop` assign each value of array in `var str`. Something like that `var str=values[i]` – laaposto Dec 28 '13 at 23:02
  • Also, what things would I need to change to make it fit what I'm using? I tried only changing the word str to values[0], just to see if that would work for at least the fisrt object, but there is something else wrong that is causing my program to crash. – CPC Dec 28 '13 at 23:08
  • var str=values[0] should work. I can not figure out why your programm crush without seeing it. – laaposto Dec 28 '13 at 23:10
  • That works, but then I tried using 123 as an example, and it said "one-hundred NaNNaN" I looked through it, but can't see anything wrong. Do you know? – CPC Dec 28 '13 at 23:13
  • Sorry, I just figured out my mistake on the last question, so you can forget that. – CPC Dec 28 '13 at 23:15
  • I cant see something wrong. It works fine for me. See http://jsfiddle.net/9JMFX/1/ – laaposto Dec 28 '13 at 23:15
0

var numericValue = 972;

convert the number to a string first:

var stringValue=numericValue+'';

then you can use sub strings to find the individual values eg.:

var hundreds = stringValue.substr(0,1);

Where the first parameter (0) is the start position and the second (1) is the number of characters in the string you're searching for.

then you can pick the string you're looking for from an object.:

var hundredsObj = {
'9':'nine hundred',
'8':'eight hundred',
'7':'seven hundred',
'6':'six hundred.....'}

var hundredsStr = hundredsObj.hundreds

andrew
  • 9,313
  • 7
  • 30
  • 61