Hey guys been working on a java Program. I am having a problem with my percent rounding up to 7% instead displaying 6.5% any ideas here is my Program.

import javax.swing.JOptionPane;
 import java.math.*;
 import java.text.NumberFormat;
public class Week_three_number_eleven {
    
    public static void main(String[] args) {
    
    String Investment = JOptionPane.showInputDialog(null,"Please enter the amount you would like to invest");
    String IntrestRate = JOptionPane.showInputDialog(null,"Now please enter you desired intrest rate(Example .065 = 6.5%");
    
    double Investment2 = Double.parseDouble(Investment);
	double IntrestRate2 = Double.parseDouble(IntrestRate);
    NumberFormat percentFormat = NumberFormat.getPercentInstance();
    JOptionPane.showMessageDialog(null,"You choose $"+Investment+".00 at the intrest rate of " + percentFormat.format(IntrestRate2));
	double ABS_Future5;
	double ABS_Future10;
	double ABS_Future20;	
	double FUTURE5 = Investment2 * Math.pow(1 + IntrestRate2,5.0);
	double FUTURE10 = Investment2 * Math.pow(1 + IntrestRate2,10.0);
	double FUTURE20 = Investment2 * Math.pow(1 + IntrestRate2,20.0);
	ABS_Future5 = Math.round(FUTURE5);
	ABS_Future10 = Math.round(FUTURE10);
	ABS_Future20 = Math.round(FUTURE20);
	JOptionPane.showMessageDialog(null,"Your investment in 5 years will be $"+ABS_Future5); 
	JOptionPane.showMessageDialog(null,"Your investment in 10 years will be $"+ABS_Future10);
	JOptionPane.showMessageDialog(null,"Your investment in 20 years will be $"+ABS_Future20);	
    }
}

AT this point LINE 14 (JOptionPane.showMessageDialog(null,"You choose $"+Investment+".00 at the intrest rate of " + percentFormat.format(IntrestRate2));)

it is suppose to display the percent the user put in but it rounds it up on the viewing screen, im guessing it is an easy fix but i just cant see it.

Thanks! hope someone knows!

in which dialogbox do you mean?
if it's in the last ones, you do know that the .round() method does that, right?

Yes ik that i mean up on line 14 that box that confirms what the user put in

what are the numbers you enter and in which order?

looks like its that method from NumberFormat that's doing it.
why do you use it anyway? if you want to format it to look like 6.5% you'll have to go another way

Edited 4 Years Ago by stultuske: added a comment

well, formatting the percentage by that NumberFormat method isn't working the way you want. why don't you write a bit of a formatting on that double? it's less than one line of code to write.

im entering .065 which should be 6.5% but it is displaying 7% even

check this DecimalFormat class which will hopefully make the other digits after the dot show:http://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html and this:http://www.exampledepot.com/egs/java.text/FormatNum.html but i do agree with stultuske just multiply by 100 and you'll have the percentage rather then using a whole format. the format i showed is to show decimals

Edited 4 Years Ago by DavidKroukamp: n/a

ok thank you got just pulled out the percent format and * by 100. works perfectly.

ok thank you got just pulled out the percent format and * by 100. works perfectly.

see? no reason to use techniques you're not familiar with if there's a simple way to solve the issue, is there? :)
(of course, learning alternatives is never bad ;) )

This question has already been answered. Start a new discussion instead.