Results 1 to 6 of 6
  1. #1
    gotenks05 is offline Member
    Join Date
    Sep 2008
    Posts
    34
    Rep Power
    0

    Default get percentage formatting to display 2 decimal places

    Alright, I've been making an update to one of my programs that allows the user to enter certain input in two ways. Since this has nothing to do with the purpose of the program and more of how it displays things, I won't discuss its purpose in this post. What I want the program to display is a percentage with two decimal places. Now, when I tried to format a decimal as a percent before, Java displayed something like 0.0775 as 8%. How do I make Java display a decimal like that or say 0.0875 as "7.75%" or "8.75%"?
    Last edited by gotenks05; 09-26-2008 at 01:13 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    when I tried to format a decimal as a percent before, Java displayed something like 0.0775 as 8%.
    What class did you use before? It looks like it just needs a tweek to give you a couple of decimal places. What does the API doc say?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm no pro at this, but my interpretation of the NumberFormat PercentInstance and also the String Formatter leads me to think that neither of these tools to represent percentages allow decimal points. I'm wondering if he has to create his own formatter to do this.

  4. #4
    gotenks05 is offline Member
    Join Date
    Sep 2008
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    What class did you use before? It looks like it just needs a tweek to give you a couple of decimal places. What does the API doc say?
    As far as I can tell I imported java.text.*; because the program formats some numbers into currency. As for the API docs, I've been trying to look that up and found nothing useful, at least through a Google search (not really sure the exact address of Sun's API documentation). Also, the book that told me how to format numbers did not discuss anything about the API, in that section at least.

    Quote Originally Posted by Fubarable View Post
    I'm no pro at this, but my interpretation of the NumberFormat PercentInstance and also the String Formatter leads me to think that neither of these tools to represent percentages allow decimal points. I'm wondering if he has to create his own formatter to do this.
    If I do need to create my own formatter, how would I do that?
    Last edited by gotenks05; 09-26-2008 at 07:37 AM.

  5. #5
    roreb1313 is offline Member
    Join Date
    Mar 2009
    Posts
    1
    Rep Power
    0

    Default Re: get percentage formatting to display 2 decimal places

    I had the same issue and I think I got it to work:

    --------------

    import java.text.NumberFormat;

    //instance variable:
    private static NumberFormat percent = NumberFormat.getPercentInstance();

    //inside any method or event:

    myAprValue = userInput / 100;
    percent = new DecimalFormat("0.0#%");

    String st = percent.format(myAprValue);

  6. #6
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    regardless, i'm sure that you're gonna have to convert your number to a percentage on your own by multiplying by 100. other than that, you can always round off to two decimal places before printing on your own. i know that java now has a printf methods, which i haven't used, but would be easy to round it off (if it's the same as c) with a %.2d

    printf Method**Development**Java Tutorial

Similar Threads

  1. round to two decimal places
    By javaMike in forum New To Java
    Replies: 3
    Last Post: 12-24-2011, 02:01 AM
  2. rounding double to two decimal places
    By javaMike in forum Advanced Java
    Replies: 15
    Last Post: 03-10-2010, 12:04 AM
  3. numbers with two decimal places
    By little_polarbear in forum New To Java
    Replies: 8
    Last Post: 08-27-2008, 11:04 PM
  4. Capping decimal places
    By Rageagainst20 in forum New To Java
    Replies: 1
    Last Post: 12-20-2007, 09:28 PM
  5. Data formatting for the <display:table>
    By yuchuang in forum Web Frameworks
    Replies: 3
    Last Post: 12-14-2007, 10:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •