Formating a String in JAVA

T

Terren

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
 
M

Murray

Terren said:
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
 
G

Gary Labowitz

Terren said:
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));
}
}
 
T

Terren

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
 
T

Tilman Bohn

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
 
T

Terren

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
 
T

Tilman Bohn

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top