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 6 of 6

Thread: How to print output in 2 decimal points in java?

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to print output in 2 decimal points in java?

    import java.io.*;
    public class Main {

    /**
    * Make by Lee Jia Ren
    */
    public static void main(String[] args) throws IOException
    {
    BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in));

    String blockNumber, flatNumber, name;
    String str;
    int unit;

    System.out.print("Enter Your Block Number: ");
    blockNumber = stdin.readLine();

    System.out.print("\nEnter Your Flat Number: ");
    flatNumber = stdin.readLine();

    System.out.print("\nEnter Your Name: ");
    name = stdin.readLine();

    System.out.print("\nEnter Eletricity Unit: ");
    str = stdin.readLine();
    unit = Integer.parseInt(str);

    EletricBillGenerator ebGen = new EletricBillGenerator();
    ElectricBill eb = ebGen.getData(blockNumber, flatNumber, name, unit );
    ElectricBill calcEB = ebGen.calc(eb);
    ebGen.display(calcEB);
    }

    }



    public class EletricBillGenerator {

    public ElectricBill getData(String blockNumber, String flatNumber, String name, int unit){
    ElectricBill eb = new ElectricBill();
    eb.setBlockNumber(blockNumber);
    eb.setFlatNumber(flatNumber);
    eb.setName(name);
    eb.setUnit(unit);

    return eb;

    }

    public ElectricBill calc(ElectricBill inElecBill){

    double billAmount;

    if (inElecBill.getUnit() < 100)
    {
    billAmount = 0.40*inElecBill.getUnit();
    }
    else if (inElecBill.getUnit() < 300)
    {
    billAmount = 0.50*inElecBill.getUnit();
    }
    else
    {
    billAmount = 0.60*inElecBill.getUnit();
    }

    billAmount = billAmount>150?billAmount*1.15:billAmount;
    inElecBill.setBillAmount(billAmount);

    return inElecBill;

    }

    public void display(ElectricBill inElecBill){
    System.out.println("");
    System.out.println("");
    System.out.println("\tElectricity Bill");
    System.out.println("****************************** ************************");
    System.out.println("\tName : " + inElecBill.getName());
    System.out.println("\tUnits : " + inElecBill.getUnit());
    System.out.println("\tTotal Charges : RM" +inElecBill.getBillAmount());
    System.out.println("****************************** ************************");

    }
    }


    public class ElectricBill {
    private int unit;
    private double billAmount;
    private String blockNumber, flatNumber, name;

    public String getBlockNumber() {
    return blockNumber;
    }
    public void setBlockNumber(String blockNumber) {
    this.blockNumber = blockNumber;
    }
    public String getFlatNumber() {
    return flatNumber;
    }
    public void setFlatNumber(String flatNumber) {
    this.flatNumber = flatNumber;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public int getUnit() {
    return unit;
    }
    public void setUnit(int unit) {
    this.unit = unit;
    }
    public double getBillAmount() {
    return billAmount;
    }
    public void setBillAmount(double billAmount) {
    this.billAmount = billAmount;
    }
    }

    java.png
    Last edited by Deep_4; November 7th, 2012 at 12:50 PM.


  2. #2
    Junior Member
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how i want to change my answer to 2 decimal place....... help urgent

    and how to make it display if he or she get the discount.......

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: how i want to change my answer to 2 decimal place....... help urgent

    Quote Originally Posted by DrakeLee View Post
    answer to 2 decimal place...
    Here's the thing: Java makes it "easy" to print numbers if you don't care what they look like. If you want them formatted to a particular number of digits after the decimal point there are several ways.

    One way is to use printf() instead of print() or println(). For your problem you need to know about the "%f" format specifier.

    Here's some code that shows what I mean. Make a little demo program and put this in your main() method.
            double amount = 123.456789123;
     
            // Java println() picks a generic format that it considers appropriate
            // Prints as many digits as it needs
            //
            System.out.println("Method 1: Amount = " + amount);
     
            // Default printf("%f") will give six digits after the decimal point
            //
            System.out.printf("Method 2: Amount = %f\n", amount);
     
            // Specify a particular number of decimal places and
            // it will round off to that number.
            //
            System.out.printf("Method 3: Amount = %.2f\n", amount);

    Output:

    Method 1: Amount = 123.456789123
    Method 2: Amount = 123.456789
    Method 3: Amount = 123.46


    Note that the print statements do not change the internal value of the variable, they just display it to your specifications.


    Reference: Java Formatter Class

    Now, serious applications involving decimal fractions of money are very likely not use floating point arithmetic at all, but for starters, rounding off a floating point number to a particular number of decimal places for printing can be done relatively painlessly with printf().


    Cheers!

    Z
    Last edited by Zaphod_b; October 30th, 2012 at 03:42 PM.

  4. #4
    Member
    Join Date
    Dec 2009
    Location
    UK
    Posts
    58
    My Mood
    Sleepy
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: how i want to change my answer to 2 decimal place....... help urgent

    I didn't know you could use printf - I generally use System.out.format("%.2f", 1.234);

    Regards,

    Shaun.

  5. #5
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: how i want to change my answer to 2 decimal place....... help urgent

    Quote Originally Posted by ShaunB View Post
    I didn't know you could use printf - I generally use System.out.format("%.2f", 1.234);
    I don't know of any differences between the two. The Java Formatter Class documentation refers to both of them as "convenience methods." From that, I conclude (and observe) that there are various less-convenient ways...

    public class Z {
        public static void main(String [] args) {
            double x = 1.234;
            System.out.format("Method 1: x = %.2f\n", x);
            System.out.printf("Method 2: x = %.2f\n", x);
        }
    }

    C programmers who have been sentenced to spend purgatory time on Planet Java tend to use printf. Java programmers with no baggage from previous incarnations tend to use whatever the heck they see in programming assistance forums like this.

    As my dear old grannie used to say, "Chacun à son goût!"


    Regards,

    Dave
    Last edited by Zaphod_b; October 30th, 2012 at 04:47 PM.

  6. #6
    Member
    Join Date
    Dec 2009
    Location
    UK
    Posts
    58
    My Mood
    Sleepy
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: how i want to change my answer to 2 decimal place....... help urgent

    C is okay with me, as is Java. People who worry about the syntax spend too long learning new languages.

    Regards,

    Shaun.

Similar Threads

  1. One Decimal Place
    By Akirien in forum Object Oriented Programming
    Replies: 5
    Last Post: August 22nd, 2012, 01:11 AM
  2. How do I change my output to 1 decimal place?
    By dunnage888 in forum What's Wrong With My Code?
    Replies: 16
    Last Post: November 3rd, 2011, 10:39 AM
  3. Pls answer( urgent)
    By sar in forum Java Theory & Questions
    Replies: 1
    Last Post: September 20th, 2011, 12:28 PM
  4. Need urgent help regarding java word wrap function.. URGENT
    By coldice in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 16th, 2011, 06:43 AM
  5. Help please - urgent (Project due and not done) - Easy answer
    By Bagzli in forum Java Theory & Questions
    Replies: 1
    Last Post: March 2nd, 2011, 06:36 PM