Java DecimalFormat format question

alfa147x

Lifer
Jul 14, 2005
29,307
105
106
Why can't I do this:

DecimalFormat twoDecimal = new DecimalFormat("000,000,000.00");

This compiles:
DecimalFormat twoDecimal = new DecimalFormat("000.00");


I keep getting this error:
Exception in thread "main" java.lang.NumberFormatException: For input string: "000,015,000.00"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224)
at java.lang.Double.parseDouble(Double.java:510)
at Tuition.main(Tuition.java:30)



All of my code if needed:
Code:
import java.text.DecimalFormat;
import javax.swing.JOptionPane;

public class Tuition {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
	    double tuitionStart;
	    int year;
	    int endYears;
	    double totalTuition = 0;
	    double[] tuitionYear;
	    String outputString = "";
	    String outputStringPlus10 = "";
	    year = 2010;
	    tuitionStart = 10000;
	    endYears = 2025;
	    tuitionYear = new double[endYears];
	    
	    DecimalFormat twoDecimal = new DecimalFormat("000.00"); 
	    //twoDecimal.applyPattern("000,000.00");

	    for(int n=0; n<15; n++){
	    outputString += year + "  $" + tuitionStart + "\n";
	    tuitionYear[year] = tuitionStart;
	    tuitionStart = tuitionStart + tuitionStart*.5;
	    tuitionStart = Double.parseDouble(twoDecimal.format(tuitionStart)); 
	    year+=1;
	    	    
	    }
	    year = 2020;
	    for(int k=0; k<4; k++){
	    	outputStringPlus10 += "Year "+ year +  "  $" + tuitionYear[year] + "\n";
	    	totalTuition =+ tuitionYear[year];
		    year+=1;
		    	    
		    }
	    JOptionPane.showMessageDialog(null,"Year  Tuition \n" + outputString + "\nCollege Tuition in 10 years \n" + outputStringPlus10 + "\nTotal Tuition: \n" + totalTuition, "College Tuition",JOptionPane.INFORMATION_MESSAGE);
	}

}

Thanks a lot for any help,
Alfa147x
 

Doublejr

Senior member
Jul 25, 2004
205
0
0
I have no idea but your could try to pass it #,#00.0# instead of 000,000,000.00 when you initiate the new formatter.

If that doesn't work you could always cast it to a string and parse it for the display, it's not pretty but it gets the job done.
 

Schmide

Diamond Member
Mar 7, 2002
5,682
912
126
This looks kind of funny to me.

Code:
totalTuition [COLOR="Red"]=+[/COLOR] tuitionYear[year];
 

alfa147x

Lifer
Jul 14, 2005
29,307
105
106
I have no idea but your could try to pass it #,#00.0# instead of 000,000,000.00 when you initiate the new formatter.

If that doesn't work you could always cast it to a string and parse it for the display, it's not pretty but it gets the job done.

Nope that didn't work

Exception in thread "main" java.lang.NumberFormatException: For input string: "15,000.0"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224)
at java.lang.Double.parseDouble(Double.java:510)
at Tuition.main(Tuition.java:31)


I know how to parse it as a string but where do I go from there?
 

alfa147x

Lifer
Jul 14, 2005
29,307
105
106
This looks kind of funny to me.

Code:
totalTuition [COLOR="Red"]=+[/COLOR] tuitionYear[year];


Is that not the same thing as:
Code:
totalTuition [COLOR="Red"]=[/COLOR]totalTuition [COLOR="Red"]+[/COLOR] tuitionYear[year];
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,566
4,481
75
Nope.

x = x+1;
is the same as
x += 1;

but

x =+ 1;
is the same as
x = +1; // as opposed to -1

Probably compiles, but it's a no-op.
 

alfa147x

Lifer
Jul 14, 2005
29,307
105
106
Nope.

x = x+1;
is the same as
x += 1;

but

x =+ 1;
is the same as
x = +1; // as opposed to -1

Probably compiles, but it's a no-op.

Interesting... I must have worked on my code last night because I just checked and it's:

Code:
	    	totalTuition = totalTuition + tuitionYear[year];

No wonder it's been compiling...
 

alfa147x

Lifer
Jul 14, 2005
29,307
105
106
Wait, what? Are you trying to round to two decimal places? Have you ever heard of fixed-point arithmetic?

Well I used
import java.text.DecimalFormat;
because it was required

In any case, parseDouble can't parse numbers with commas. (Strange that I can't find that in the API.)

Thanks a bunch I fixed it:
Code:
import java.text.DecimalFormat;
import javax.swing.JOptionPane;

public class Tuition {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
	    double tuitionStart;
	    int year;
	    int endYears;
	    double totalTuition = 0;
	    double[] tuitionYear;
	    String outputString = "";
	    String outputStringPlus10 = "";
	    year = 2010;
	    tuitionStart = 10000;
	    endYears = 2025;
	    tuitionYear = new double[endYears];
	    
	    DecimalFormat twoDecimal = new DecimalFormat("000.00"); 
	    DecimalFormat twoDecimalComma = new DecimalFormat("#,#00.00"); 

	    //twoDecimal.applyPattern("000,000.00");

	    for(int n=0; n<15; n++){
	    outputString += year + "  $" + twoDecimalComma.format(tuitionStart) + "\n";
	    tuitionYear[year] = tuitionStart;
	    tuitionStart = tuitionStart + tuitionStart*.5;
	    tuitionStart = Double.parseDouble(twoDecimal.format(tuitionStart)); 
	    year+=1;
	    	    
	    }
	    year = 2020;
	    for(int k=0; k<4; k++){
	    	outputStringPlus10 += "Year "+ year +  "  $" + twoDecimalComma.format(tuitionYear[year]) + "\n";
	    	totalTuition = totalTuition + tuitionYear[year];
		    year+=1;
		    	    
		    }
	    JOptionPane.showMessageDialog(null,"Year  Tuition \n" + outputString + "\nCollege Tuition in 10 years \n" + outputStringPlus10 + "\nTotal Tuition: \n" + "$" +twoDecimalComma.format(totalTuition), "College Tuition",JOptionPane.INFORMATION_MESSAGE);
	}

}
 
Last edited:

Doublejr

Senior member
Jul 25, 2004
205
0
0
I know how to parse it as a string but where do I go from there?

That all depends on where you are displaying it :D

But you could do a String x = double.toString();

then put it in a for loop to pad the zeros, then display the string

x = "0" + x;

or something like that, with conditionals if you need a different digit in a certain location or your comma separator.

Like I said it isn't pretty but you can make it work.

also in the new formatter you might try just ####,####,####.## as the parameter, I just glanced at the docs but am not 100% of the arguments lol.
 

alfa147x

Lifer
Jul 14, 2005
29,307
105
106
That all depends on where you are displaying it :D

But you could do a String x = double.toString();

then put it in a for loop to pad the zeros, then display the string

x = "0" + x;

or something like that, with conditionals if you need a different digit in a certain location or your comma separator.

Like I said it isn't pretty but you can make it work.

also in the new formatter you might try just ####,####,####.## as the parameter, I just glanced at the docs but am not 100% of the arguments lol.

Thanks for the help!
I did get it working above, i was trying to assign a double a string.