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

Thread: Setting what currency i want with getCurrencyInstance()

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Setting what currency i want with getCurrencyInstance()

    Hi all, like in my post title, how do i set an alternative currency to print out.

    For example with the following program it is printing out in format. Im guessing this is to do with my Locale and the fact Im in the UK.

    How would i print this out with the dollar sign then? without changing any settings.

    Thanks

     
    // ask user how many quarters, dimes and nickels they have
    // print out how much money the user has, format the output to display currency symbol
     
    import java.util.Scanner;
    import java.text.NumberFormat;
     
    public class usaCurrencyAdd {
     
        public static void main(String[] args) {
     
            final double quarterValue = 0.25;
            final double dimeValue = 0.10;
            final double nickelValue = 0.05;
            int quartersQuantity, dimesQuantity, nickelsQuantity;
            double quartersTotal, dimesTotal, nickelTotal, grandTotal;
     
            Scanner scan = new Scanner(System.in);
     
            System.out.println("How many quarters do you have?");
            quartersQuantity = scan.nextInt();
     
            System.out.println("How many dimes do you have?");
            dimesQuantity = scan.nextInt();
     
            System.out.println("How many nickels do you have?");
            nickelsQuantity = scan.nextInt();
     
            quartersTotal = quarterValue * quartersQuantity;
            dimesTotal = dimeValue * dimesQuantity;
            nickelTotal = nickelValue * nickelsQuantity;
            grandTotal = quartersTotal + dimesTotal + nickelTotal;
     
            NumberFormat fmt = NumberFormat.getCurrencyInstance();
     
            System.out.println(fmt.format(grandTotal));
    }
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,511
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Setting what currency i want with getCurrencyInstance()

    Two options:

    1. Create your own Currency object based upon a Locale of your choice.
    2. Use DecimalFormat where you can specify the format: "$#.##" or whatever you like.
    Improving the world one idiot at a time!

Similar Threads

  1. Setting Up a map using jstl
    By kunal_00731 in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: September 25th, 2011, 03:00 PM
  2. Error in setting node value
    By praveen.8721 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 12th, 2011, 07:09 AM
  3. JFileChooser setting directory
    By fox in forum AWT / Java Swing
    Replies: 2
    Last Post: February 18th, 2011, 10:58 AM
  4. Confused about setting up JSP
    By mjpam in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: September 17th, 2010, 05:14 AM
  5. Setting up, using DataSources
    By jasonm in forum JDBC & Databases
    Replies: 1
    Last Post: March 4th, 2010, 09:37 PM