Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By Junky

Thread: how to change numbers into word numbers?

  1. #1
    akeni is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default how to change numbers into word numbers?

    Sorry if my title is ambiguous. I don't know how to explain it in a short sentence :D.

    So if i input some random integer:1003 as a string.

    How can i get the output to look like this:

    1,003

    and

    one thousand and three.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: how to change numbers into word numbers?

    For the first one? Look at the NumberFormat class.

    For the second one? How do you do that in your head?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default Re: how to change numbers into word numbers?

    Here's something to start you off:

    Java Code:
    final String[] nums0To19 = {"zero", "one", "two", /* ... */ "nineteen"};
    public void speakNum(int num) {
        System.out.println(nums0To19[num]);
    }
    Modify that to deal with tens, hundreds, thousands and so forth.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: how to change numbers into word numbers?

    Also look at division to get the number of 1000's for example.

  5. #5
    akeni is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Re: how to change numbers into word numbers?

    okay thanks for the suggestions so far. Will try it out right now

  6. #6
    akeni is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Re: how to change numbers into word numbers?

    okay i'm still stuck. Right now, i have the digits in a character array box.

    eg. if the input was 123,

    it would be held in a char [] number = (1,2,3) box.

    i can't figure how how to put seperators with the number format class, and i can't figure how how to tell the size of each digit. (I got it to print one two three for the input 123).

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: how to change numbers into word numbers?

    In what format are you getting the input? As a String: "123" or as an int: 123 or as three chars: '1', '2', '3'?
    What statement(s) are you using to get the input?

  8. #8
    kevinn205 is offline Member
    Join Date
    Nov 2011
    Posts
    65
    Rep Power
    0

    Default Re: how to change numbers into word numbers?

    Many replys here though i've noticed noone gave you a definative answer to exactly what your looking for;

    best described I think I would say you are trying to convert a integer into a numeric string and also asking how to format an integer to a string in words

    I'll try to look into what your trying to do and see if I can find anything, personnaly i've never found a need to convert a number into the word equivalent-- best of luck

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: how to change numbers into word numbers?

    Quote Originally Posted by akeni View Post
    i have the digits in a character array
    Leave it as an int (best option) or a String.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: how to change numbers into word numbers?

    Quote Originally Posted by kevinn205 View Post
    noone gave you a definative answer to exactly what your looking for;
    If you mean no-one spoonfed them the answer then no that is not how it works. They were given plenty of tips and hints on how to do it themselves. Not only will they have done the work themselves (and not cheated) but they will also learn from the experience.
    Norm likes this.

  11. #11
    akeni is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Re: how to change numbers into word numbers?

    it was a string input, but i returned it into a character. It cannot be in an int type because it the number can be greater than int's capacity.

  12. #12
    lakshmit is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default Re: how to change numbers into word numbers?

    Hi,

    Please check the below code.
    import java.text.DecimalFormat;
    public class ConvertIntToString {
    public static void main(String[] args) {

    String aString = "1003";
    int aInt= Integer.parseInt(aString);
    System.out.println(aInt);
    DecimalFormat df = new DecimalFormat("#,##0;(#,##0)");
    System.out.println(df.format(aInt));
    }
    }

    Thanks
    Lakshmi M

  13. #13
    lakshmit is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default Re: how to change numbers into word numbers?

    Hi,

    Below is the program to convert an integer into words.

    [ spoonfeeding deleted - Jos ]

    Regards
    Lakshmi M
    Last edited by JosAH; 11-17-2011 at 10:45 AM. Reason: spoonfeeding ...

  14. #14
    akeni is offline Member
    Join Date
    Oct 2011
    Posts
    31
    Rep Power
    0

    Default Re: how to change numbers into word numbers?

    thanks for the help, i'm still figuring it out. Almost got it though :)

Similar Threads

  1. how to count numbers under 5.5
    By pinotje in forum New To Java
    Replies: 10
    Last Post: 05-10-2011, 06:53 PM
  2. Replies: 11
    Last Post: 01-14-2011, 07:36 PM
  3. Sum of 4 numbers...!?
    By xmenus in forum New To Java
    Replies: 7
    Last Post: 02-14-2010, 04:36 PM
  4. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-15-2008, 12:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •