Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Formating a String in JAVA

Reply
Thread Tools

Formating a String in JAVA

 
 
Terren
Guest
Posts: n/a
 
      12-13-2004
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

 
Reply With Quote
 
 
 
 
Murray
Guest
Posts: n/a
 
      12-13-2004

"Terren" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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


 
Reply With Quote
 
 
 
 
Gary Labowitz
Guest
Posts: n/a
 
      12-13-2004
"Terren" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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


 
Reply With Quote
 
Terren
Guest
Posts: n/a
 
      12-13-2004
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

 
Reply With Quote
 
Tilman Bohn
Guest
Posts: n/a
 
      12-13-2004
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

 
Reply With Quote
 
Terren
Guest
Posts: n/a
 
      12-13-2004
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

 
Reply With Quote
 
Tilman Bohn
Guest
Posts: n/a
 
      12-13-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
easy string formating question f pemberton Python 5 08-11-2006 10:48 AM
Formating a string John Smith ASP .Net 5 04-08-2006 11:02 PM
Formating String To Paragraph Format questionmarc420@msn.com Java 6 01-20-2006 10:17 PM
String Formating Java 1 06-07-2005 12:12 AM
Formating a String in JAVA Terren Java 6 12-13-2004 09:54 AM



Advertisments