aspose file tools*
The moose likes Java in General and the fly likes Help! With the formatting of the percent! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Help! With the formatting of the percent!" Watch "Help! With the formatting of the percent!" New topic
Author

Help! With the formatting of the percent!

Doug Con
Greenhorn

Joined: Feb 14, 2010
Posts: 7
I just started doing java and one of my assignments require the format for the percent to be displayed but yet when I format it to display as a percent it reverts to just a nondecimal. What am I doing wrong. I have it print prior to format and all looks good(.0525) but when applying the format it shows 5% where is the 5.25%

Anyone?

Thanks



package project3_1;

import java.util.Scanner;
import java.text.NumberFormat;
import java.math.*;

public class Project3_1 {

public static void main(String[] args) {
// Create a Scanner Object and While Loop
Scanner sc = new Scanner(System.in);
String choice ="y";

while (choice.equalsIgnoreCase("y"))
{
// Get Input from user
System.out.print("Enter Loan Amount: ");
double loanamount = sc.nextDouble();
System.out.print("Enter Interest Rate: ");
double interestrate = sc.nextDouble();
System.out.println(interestrate); //test print remove later

// Calculate Interest Amount
BigDecimal decimalinterestrate = new BigDecimal(interestrate);
decimalinterestrate = decimalinterestrate.setScale(3, RoundingMode.HALF_UP);
double interestamount = loanamount * interestrate;
System.out.println(decimalinterestrate); // test print remove later

// Print out data
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
String message =
"Loan Amoount: " + currency.format(loanamount) + "\n"
+ "Interest rate: " + percent.format(decimalinterestrate)+"\n"
+ "Interest: " + currency.format(interestamount) + "\n";
System.out.println(message);

// See if they want to continue
System.out.print("Continue? (y/n): ");
choice = sc.next();
System.out.println();

}
}
}
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19755
    
  20

Please Use Code Tags.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Doug Con
Greenhorn

Joined: Feb 14, 2010
Posts: 7
I have not learned that yet online and I think the instructor is looking for something that would be befitting a new programmer.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19755
    
  20

What are you talking about? I asked you to use code tags when you post code here in these forums. That will make your code easier to read.
Doug Con
Greenhorn

Joined: Feb 14, 2010
Posts: 7
Missunderstood, my first post.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

While the Javadocs aren't entirely clear, you'll probably need to not use the default percentage NumberFormat if it doesn't meet your requirements.
Doug Con
Greenhorn

Joined: Feb 14, 2010
Posts: 7
The requirements of the assignment require the use of the bigdecimal for the percent. I think this is where the problem is coming from. I placed 2 print statements to show that the decimal is correct but when it goes into the display part of the progam the percent format changes. Is there a way to format the bigdecimal so that it will print all of the interestrate and not just round it.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18962
    
  40

I am not convinced that the issue is with BigDecimal. I think the issue may be with the NumberFormat class. As David mentioned, there is nothing in the JavaDoc that says that it supports anything but whole percents. It may support it, but the JavaDoc doesn't show it.

Anyway, how about using the DecimalFormat class instead?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Doug Con
Greenhorn

Joined: Feb 14, 2010
Posts: 7
Do you think it could be the getPercent? We have not used the decimalformat class yet. So I don't think that is what he wants.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18962
    
  40

Doug Con wrote:Do you think it could be the getPercent? We have not used the decimalformat class yet. So I don't think that is what he wants.



Okay, just played with it a bit... and got it working. Take a look at the setMinimumFractionDigits() method.

Henry
Doug Con
Greenhorn

Joined: Feb 14, 2010
Posts: 7
Took a little while but I figured it out with your help. Thanks
Bhagat Singh Rawat
Ranch Hand

Joined: Apr 04, 2009
Posts: 93
Edited to remove answer.


Brainbench Java 2.0 Fundamentals, J2EE 1.4
http://www.brainbench.com/transcript.jsp?pid=8192792
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please don't provide direct answers. Our goal at JavaRanch is to help people to learn, and to struggle through problems on their own. We are NotACodeMill, and we want people to DoYourOwnHomework. (Well, *their* own, but I want the link :)
Bhagat Singh Rawat
Ranch Hand

Joined: Apr 04, 2009
Posts: 93
David Newton wrote:Please don't provide direct answers. Our goal at JavaRanch is to help people to learn, and to struggle through problems on their own. We are NotACodeMill, and we want people to DoYourOwnHomework. (Well, *their* own, but I want the link :)



Sorry David!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help! With the formatting of the percent!