• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help! With the formatting of the percent!

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();

}
}
}
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please Use Code Tags.
 
Doug Con
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Missunderstood, my first post.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Doug Con
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Took a little while but I figured it out with your help. Thanks
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Edited to remove answer.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic