Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert Number To Character String Representation

 
chris hayden
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
user will enter the number 44 and will be returned forty-four
any suggestions on which classes or methods to use?
 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the user enters it, it will already be in String form. Normally to convert an integer (say x) to a String, you can just use:
String s = "" + x;
and to convert a String to an integer:
x = Integer.parseInt(s);
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He needs to turn:
32 -> thirty-two
75 -> seventy-five
It's not too complicated. You can use the String method toCharArray to convert the string to an array of chars. Then you need to work your way through the array.
A clever way to do it would be to have an array for each unit:
String[] tens = {"ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}
Don't forget that most of the entries in the "ten" group need to be handled differently (thirteen not ten-three).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic