Reduce % size

Posted on 2011-02-18
Medium Priority
Last Modified: 2012-05-11
Please advise how to make correct percentage view with only 2 digits after comma.
now I have:
System.out.println("    Margin use = " + (u / c * 100) + "%")

 Margin use = 1.5060808012349864%

Open in new window

Question by:gbcbr
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 47

Expert Comment

ID: 34927583
I think you should use DecimalFormat like

LVL 47

Accepted Solution

for_yan earned 500 total points
ID: 34927660
so if you have it like that:

    double amount = (u / c * 100);
    NumberFormat formatter = new DecimalFormat("#0.00");
System.out.println  ("Margin Use = " + formatter.format(amount) + "%")


Author Comment

ID: 34927707
Thank you, I make it even easy
        NumberFormat formatter = new DecimalFormat("#0.00");
        System.out.println("    Margin use = " + formatter.format(u / c * 100) + "%");
LVL 47

Expert Comment

ID: 34927718
Yes, it is of course also possible

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month9 days, 1 hour left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question