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
 
X

xterm

I figured DecimalFormat could do it, but i was wrong. You will have to
do it manually, small hint towards completing it, number of spaces in
the string will be equivalent to the length of your previous string
divided by 3 (integer division).
 
T

Terren

Thanks xterm. I made this method to do it for me. It is pretty
primative but it does the job.
This is my first draft, I am going to try simplify it. I tried using
the Mod function to determine the every third digit but it did some
funny stuff. I think that it will be quick, so I will mess around with
it until I get it right.

public String SpaceNumbersForDisplay(String s){
String formattedString="";
String mIntegerPart="";
String mDecimalPart="";
int index=0;

//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

//Create a Character array of the String excluding the decimals
char c[] = mIntegerPart.toCharArray();
for(int i=c.length-1;i!=-1;i--){
//Add a Space in between every 3rd Digit from the right
if(index==3){
formattedString = c + " "+ formattedString;
index=0;
}else{
formattedString =c + formattedString ;
}//end if
index++;
}//end for
return formattedString + mDecimalPart;
} //end formatNumber
 
M

Michael Borgwardt

xterm said:
I figured DecimalFormat could do it, but i was wrong.

I don't see why it couldn't be done with DecimalFormat. Should work fine,
you just need to specify the DecimalFormatSymbols explicitly.
 
T

Terren

The problem is that I don't know what the number is it could be 1000
or 1000000000000 or 1000000000000000000 then I would have to check the
length each time and apply a different pattern.
 
M

Murray

Terren said:
The problem is that I don't know what the number is it could be 1000
or 1000000000000 or 1000000000000000000 then I would have to check the
length each time and apply a different pattern.

No you wouldn't.

String number = "1000000"; // could be anything
int num = Integer.parseInt(number);
DecimalFormat format = new DecimalFormat();
DecimalFormatSymbols s = format.getDecimalFormatSymbols();
s.setGroupingSeparator(' ');
format.setDecimalFormatSymbols(s);

String formattedNumber = format.format(num);
 
T

Tilman Bohn

The problem is that I don't know what the number is it could be 1000
or 1000000000000 or 1000000000000000000 then I would have to check the
length each time and apply a different pattern.

No. Michael pointed you in the direction to look. Please have a peek
at the API doc for DecimalFormatSymbols. You might find a method to
manipulate the (hint, hint, hint) grouping separator.

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top