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: pls help me how to add decimal format up to two places in the followin code

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default pls help me how to add decimal format up to two places in the followin code

    import javax.swing.JOptionPane;
    import java.text.DecimalFormat;
    public class Sample3 {
    public static void main(String args[]){
    double amount,iRate,monPay,totalPay;
    int years;
    String amountStr;
    String irateStr;
    String yearsStr;
    //takes input for loan amount,rate and loan period
    amountStr = JOptionPane.showInputDialog(null,"Enter loan amount $ : ");
    irateStr = JOptionPane.showInputDialog(null,"Enter % Annual Interest: ");
    yearsStr = JOptionPane.showInputDialog(null,"Enter the loan Period; ");

    amount = Double.parseDouble(amountStr); //convert input String into double
    iRate = Double.parseDouble(irateStr); //convert input string into double
    years = Integer.parseInt(yearsStr); //convert input string into integer

    monPay = (amount * iRate / 1200) /
    ( 1 - Math.pow( 1 / ( 1 + iRate / 1200), 12 * years)) ;//calculate monthly payment
    totalPay = monPay * 12 * years;

    DecimalFormat df = new DecimalFormat("0.00");
    System.out.println(df.format(monPay));


    JOptionPane.showMessageDialog(null,"your monthly payment is: " + monPay + "\n" +
    "your total payment is: " + totalPay );
    }

    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: pls help me how to add decimal format up to two places in the followin code

    Please post your code correctly per the link I provided you in your intro thread.

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: pls help me how to add decimal format up to two places in the followin code

    oh sorry..m newbie in this type of forum so cudnt get along well!!

Similar Threads

  1. How do I get my answers to out with 2 decimal places?
    By dunnage888 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 8th, 2012, 12:23 PM
  2. New to Java Need 2 decimal places, please :). Here is my code
    By Charyl in forum Member Introductions
    Replies: 2
    Last Post: June 28th, 2011, 03:06 AM
  3. Java program to format a double value to 2 decimal places
    By JavaPF in forum Java Programming Tutorials
    Replies: 3
    Last Post: December 4th, 2010, 04:08 PM
  4. Java program to format a double value to 2 decimal places
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 3
    Last Post: December 4th, 2010, 04:08 PM
  5. [SOLVED] How to use decimal place when formatting an output?
    By napenthia in forum Java Theory & Questions
    Replies: 2
    Last Post: April 27th, 2009, 03:17 AM

Tags for this Thread