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

Thread: Simple curency convertor

  1. #1
    Junior Member reddevilggg's Avatar
    Join Date
    Jan 2011
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple curency convertor

    I'm a Java beginner, i'm writing a simple program to convert pounds to euros, but i would like the output answer to be rounded up to 2 significant figures, can anybody help. here is the code

    import java.util.Scanner;
    public class EuroConversion {
    public static void main(String[] args) {
    Scanner input = new Scanner (System.in);

    System.out.println("Enter the amount of pounds to convert");
    float money = input.nextInt();

    double convertion = money * 1.14;


    System.out.println("The convertion is " + convertion + " Euros");


    }
    }

    Thanks for any help.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Simple curency convertor

    i would like the output answer to be rounded up to 2 significant figures,
    Look at the DecimalFormat class.
    And also the printf method.

  3. #3
    Junior Member shia's Avatar
    Join Date
    Sep 2011
    Location
    Manchester, UK
    Posts
    19
    My Mood
    Nerdy
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Simple curency convertor

    Quote Originally Posted by reddevilggg View Post
    I'm a Java beginner, i'm writing a simple program to convert pounds to euros, but i would like the output answer to be rounded up to 2 significant figures, can anybody help. here is the code

    import java.util.Scanner;
    public class EuroConversion {
    public static void main(String[] args) {
    Scanner input = new Scanner (System.in);

    System.out.println("Enter the amount of pounds to convert");
    float money = input.nextInt();

    double convertion = money * 1.14;


    System.out.println("The convertion is " + convertion + " Euros");


    }
    }

    Thanks for any help.
    Formatter (Java Platform SE 6)

  4. #4
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: Simple curency convertor

    You could use the String.format() method. It has two parameters . The first is a format identifier, the second is the output string. The identifier starts with
    the % symbol. Then you have some optional flags, the width, the significant places, and data type. Here are some examples, and you could do some
    searching on Java's String format method: Format a String (JDK1.5) - Real's Java How-to. Also you may want to look into using a named constant for
    your conversion rate.

  5. #5
    Junior Member reddevilggg's Avatar
    Join Date
    Jan 2011
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple curency convertor

    Thanks everyone :-)

  6. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple curency convertor

    package examples;

    import java.text.DecimalFormat;
    import java.util.Scanner;
    public class Main {
    public static void main(String[] args) {
    Scanner input = new Scanner (System.in);

    System.out.println("Enter the amount of pounds to convert");
    float money = input.nextFloat();

    double convertion = money * 1.14;
    DecimalFormat df=new DecimalFormat("#.##");
    System.out.println("The convertion is " + df.format(convertion) + " Euros");


    }
    }


    Use this code this will work

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Simple curency convertor

    Quote Originally Posted by pushpendra.saraswat@gmail View Post
    Use this code this will work
    You give no reason why the original poster should use that code, nor an explanation of what it does. Please read the forum rules and the following link:
    http://www.javaprogrammingforums.com...n-feeding.html

Similar Threads

  1. beginner and need simple help. please!
    By alal12 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 19th, 2010, 05:38 PM
  2. help! simple ATM
    By galva in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 9th, 2010, 01:14 AM
  3. [SOLVED] Should be simple.
    By Time in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 22nd, 2010, 02:23 AM
  4. urgent simple help for a very simple program
    By albukhari87 in forum Java Applets
    Replies: 4
    Last Post: June 5th, 2010, 03:43 PM
  5. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 03:47 AM