Formating a String in JAVA

Discussion in 'Java' started by Terren, Dec 13, 2004.

  1. Terren

    Terren Guest

    Hi

    I am relatively new to JAVA and I need to know if there is a method
    that I can call the will format a string according to what I parse. I
    need to format a number (which is actually a string) to that is the
    number is 1000000 after formating it will look like this 1 000 000.

    It doesn't even have to be a string java.string.somthing.methods()
    type function. If if someone could point me in the right direction
    Thanks in advance
     
    Terren, Dec 13, 2004
    #1
    1. Advertising

  2. Terren

    Murray Guest

    "Terren" <> wrote in message
    news:...
    > Hi
    >
    > I am relatively new to JAVA and I need to know if there is a method
    > that I can call the will format a string according to what I parse. I
    > need to format a number (which is actually a string) to that is the
    > number is 1000000 after formating it will look like this 1 000 000.
    >
    > It doesn't even have to be a string java.string.somthing.methods()
    > type function. If if someone could point me in the right direction
    > Thanks in advance
    >


    Have a look at java.text.DecimalFormat
     
    Murray, Dec 13, 2004
    #2
    1. Advertising

  3. "Terren" <> wrote in message
    news:...
    > Hi
    >
    > I am relatively new to JAVA and I need to know if there is a method
    > that I can call the will format a string according to what I parse. I
    > need to format a number (which is actually a string) to that is the
    > number is 1000000 after formating it will look like this 1 000 000.
    >
    > It doesn't even have to be a string java.string.somthing.methods()
    > type function. If if someone could point me in the right direction
    > Thanks in advance


    Perhaps something like:
    public class Formatter
    {
    public static void main( String[] args )
    {
    java.text.DecimalFormatSymbols s = new
    java.text.DecimalFormatSymbols( );
    s.setGroupingSeparator(' ');
    java.text.DecimalFormat f = new java.text.DecimalFormat( "#,000",s);
    System.out.println( f.format(1000));
    System.out.println( f.format(1000000));
    System.out.println( f.format(100000000));
    }
    }
    --
    Gary
     
    Gary Labowitz, Dec 13, 2004
    #3
  4. Terren

    Terren Guest

    Thanks everyone. I have finally settled on this method. I am posting it
    in case another newbie (Geez I hate that name) has a similar question.

    public String SpaceNumbersForDisplay(String s, char mSeparator){
    String formattedString="";
    String mIntegerPart="";
    String mDecimalPart="";
    //If the number is Zero then just return 0.00
    if(s.equals("0")){
    return "0.00";
    }//end if

    //Check if there are decimals
    if(s.indexOf(".")==-1){
    mIntegerPart=s;
    mDecimalPart=".00";
    }else{
    mIntegerPart= s.substring(0,((int)s.indexOf(".")));
    mDecimalPart= s.substring(((int)s.indexOf(".")),(int)s.length());
    }//end if

    //Get the integer form of the String
    int num = Integer.parseInt(mIntegerPart);
    //Create new DecimalFormat object
    DecimalFormat dfNum = new DecimalFormat();
    //Create new DecimalFormatSynbols object
    DecimalFormatSymbols dfsNum= dfNum.getDecimalFormatSymbols();
    //Apply the symbol
    dfsNum.setGroupingSeparator(mSeparator);
    dfNum.setDecimalFormatSymbols(dfsNum);
    //Apply the thousands separator method of DecimalFormat
    formattedString = dfNum.format(num) + mDecimalPart;
    //return the formatted string
    return formattedString;
    } //end SpaceNumbersForDisplay
     
    Terren, Dec 13, 2004
    #4
  5. Terren

    Tilman Bohn Guest

    On Mon, 13 Dec 2004 02:28:02 -0800, Terren wrote:

    > Thanks everyone. I have finally settled on this method. I am posting it
    > in case another newbie (Geez I hate that name) has a similar question.


    A few quick notes.

    > public String SpaceNumbersForDisplay(String s, char mSeparator){
    > String formattedString="";
    > String mIntegerPart="";
    > String mDecimalPart="";
    > //If the number is Zero then just return 0.00
    > if(s.equals("0")){
    > return "0.00";


    1. Unnecessary. (See below.)
    2. You're trying to normalize, but you really don't. (Pass in "0.0",
    get a different result than for "0". Unexpected for clients.)
    3. Not localized.

    > }//end if
    >
    > //Check if there are decimals
    > if(s.indexOf(".")==-1){


    1. Unnecessary (DecimalFormat handles decimal fractions just fine).
    2. Breaks if default locale doesn't use '.' as decimal separator.

    > mIntegerPart=s;
    > mDecimalPart=".00";
    > }else{
    > mIntegerPart= s.substring(0,((int)s.indexOf(".")));
    > mDecimalPart= s.substring(((int)s.indexOf(".")),(int)s.length());
    > }//end if
    >
    > //Get the integer form of the String
    > int num = Integer.parseInt(mIntegerPart);
    > //Create new DecimalFormat object
    > DecimalFormat dfNum = new DecimalFormat();
    > //Create new DecimalFormatSynbols object


    Don't document what you're doing unless it's non-obvious. Document
    why you're doing it is much more important (again, unless it's
    obvious). If anything, say why you need the DecimalFormatSymbols.

    > DecimalFormatSymbols dfsNum= dfNum.getDecimalFormatSymbols();
    > //Apply the symbol
    > dfsNum.setGroupingSeparator(mSeparator);
    > dfNum.setDecimalFormatSymbols(dfsNum);
    > //Apply the thousands separator method of DecimalFormat
    > formattedString = dfNum.format(num) + mDecimalPart;
    > //return the formatted string
    > return formattedString;
    > } //end SpaceNumbersForDisplay


    In general, it's usually best to see if the standard library can do
    what you're trying to achieve before starting to implement lots of
    special cases yourself. Chances are you'll overlook some finer points
    that are automatically taken care of in the library. This is a good
    example. There is no need to do all those decimal point gymnastics,
    and it won't work as you intended if the platform locale doesn't use
    a dot as decimal separator (a comma is customary in many parts of the
    world).

    Look again at DecimalFormat, it can do all you want to do and more
    in just a handful of lines.

    Cheers, Tilman

    --
    `Boy, life takes a long time to live...' -- Steven Wright
     
    Tilman Bohn, Dec 13, 2004
    #5
  6. Terren

    Terren Guest

    Thanks for your input. I generally over document for my own benefit
    because I have only been doing this for about 2 weeks and I am still
    getting the hanging of it and if I don't do the //end if stuff I will
    get totally lost

    At first I tried not to have the decimal gymnastics but I kept getting
    a numberFormatException error when I tried ot parse e.g.

    s=100.00
    int num =Integer.parseInterger(s);

    Thats why I did the first bit
     
    Terren, Dec 13, 2004
    #6
  7. Terren

    Tilman Bohn Guest

    On Mon, 13 Dec 2004 04:16:48 -0800, Terren wrote:

    > Thanks for your input. I generally over document for my own benefit
    > because I have only been doing this for about 2 weeks and I am still
    > getting the hanging of it and if I don't do the //end if stuff I will
    > get totally lost


    That's ok. Just remember for later: The `why' is more important than the
    `what'. Code that needs explaining usually really needs refactoring.

    > At first I tried not to have the decimal gymnastics but I kept getting
    > a numberFormatException error when I tried ot parse e.g.
    >
    > s=100.00
    > int num =Integer.parseInterger(s);


    That's because 100.00 is not an int.

    > Thats why I did the first bit


    If you want to process numbers that you expect not to be integers,
    you shouldn't be trying to parse them as integers. Have a look at
    java.lang.Double, which is to double as Integer is to int. And then
    look up NumberFormat (from which DecimalFormat inherits), specifically
    what overloaded format() methods there are. ;-)

    Cheers, Tilman

    --
    `Boy, life takes a long time to live...' -- Steven Wright
     
    Tilman Bohn, Dec 13, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Smith

    Formating a string

    John Smith, Apr 8, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    466
    Kevin Spencer
    Apr 9, 2006
  2. Terren

    Formating a String in JAVA

    Terren, Dec 13, 2004, in forum: Java
    Replies:
    6
    Views:
    426
    Tilman Bohn
    Dec 13, 2004
  3. Guest

    String Formating

    Guest, Jun 6, 2005, in forum: Java
    Replies:
    1
    Views:
    400
    Real Gagnon
    Jun 7, 2005
  4. Replies:
    6
    Views:
    4,322
    Roedy Green
    Jan 20, 2006
  5. f pemberton

    easy string formating question

    f pemberton, Aug 10, 2006, in forum: Python
    Replies:
    5
    Views:
    349
    Slawomir Nowaczyk
    Aug 11, 2006
Loading...

Share This Page