Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Percentage with String.format

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Percentage with String.format

    I'm not having great success finding an answer in google for this question.
    I wanted to try to format a double into a percentage using the String.format() method (not the number format classes). I was trying to figure out a String which would work, and the closest I've gotten is this:
    public static void main(String[] args) {
    		double value = 0.7542;
    		String format = "%.2f%%";
    		System.out.println(String.format(format, value));
    }
    This outputs:
    0.75%
    The problem, is that I am wanting it to treat my value as a percentage in decimal form, meaning the output I want to get is:
    75.42%
    Is there a format which will treat it like a percentage? If not, is it possible to do math inside of the format String (multiply the value by 100)?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Percentage with String.format

    do math inside of the format String (multiply the value by 100)?
    Did you try this:
    System.out.println(String.format(format, 100*value));

    The format String is a String of characters. There is no execution of its parts like in an expression.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Percentage with String.format

    I'm wanting to avoid that if possible. This is being put into a method where the percentage format will not always be used. I would have to add an extra if statement, which I want to avoid if possible so I don't add extra complexity.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Replies: 6
    Last Post: June 3rd, 2013, 04:57 AM
  2. Replies: 1
    Last Post: March 15th, 2013, 03:04 AM
  3. Percentage Array Help
    By Gra89 in forum What's Wrong With My Code?
    Replies: 25
    Last Post: December 8th, 2012, 10:08 PM
  4. Calculationg the percentage.
    By Purple01 in forum Java Theory & Questions
    Replies: 2
    Last Post: September 25th, 2012, 08:25 AM
  5. No Percentage?
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 18th, 2009, 12:02 PM