• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java DecimalFormat format question

alfa147x

Lifer
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
 
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.
 
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?
 
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.
 
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...
 
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:
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 😀

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.
 
That all depends on where you are displaying it 😀

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.
 
Back
Top