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: Unsure

  1. #1
    Junior Member
    Join Date
    May 2018
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Unsure

    Hi guys just wanted to know what the following represents or means in java

    %.2f

    I used it in a program I wrote and still have no idea what it actually means, the code looks like this
    import java.util.Scanner;

    /** Area.java
    * This program computes the area of a circle
    * Date created: 12 February 2018 */

    public class Area {
    public static void main(String []args) {
    //constant declaration
    final Double PI = 3.142159;

    //variable declaration
    double area, radius;

    //input
    Scanner sc = new Scanner(System.in);
    System.out.print("Please enter the radius please: ");
    radius = sc.nextDouble();

    //processing
    area = PI * radius * radius;

    //output
    System.out.printf("The area of the circle = %.2f", area);
    }
    }

  2. #2
    Junior Member
    Join Date
    May 2018
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Unsure

    That is a type of format of printf method to print in monitor
    %.2f means : the % character takes it's usual meaning here -- to substitute with a value (here, with 2 decimal places)
    that depend on your situations every place you need two float decimal number you can use that or maybe in other situation you need other format like that
    // radius is 6; the area of the circle = 113.12
    System.out.printf("num is %f\n",area );

    // radius is 6 ; the area of the circle = 113.117724
    System.out.printf("num is %.2f\n", area );
    if you don't understand my answer not problem just ask question

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Unsure

    See the Formatter class's API doc for a description of how to make format strings
    https://docs.oracle.com/javase/8/doc...Formatter.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Unsure of what happened, but it can't be good
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: May 23rd, 2012, 04:07 PM
  2. Unsure where to start with networking
    By totesmagotes in forum Java Networking
    Replies: 3
    Last Post: May 9th, 2012, 04:52 PM
  3. In a bit of a pickle, unsure of what I should do
    By MysticDeath in forum What's Wrong With My Code?
    Replies: 16
    Last Post: October 31st, 2009, 09:10 AM
  4. :!! Unsure how to set this up/ Still learning java loops!!!!!
    By raidcomputer in forum Loops & Control Statements
    Replies: 4
    Last Post: September 29th, 2009, 10:28 AM