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: Need help with Currency Symbol format

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

    Default Need help with Currency Symbol format

     
    package billing.util;
     
     
    import java.math.RoundingMode;
    import java.text.NumberFormat;
    import java.util.Scanner;
     
     
    public class CurrencyFormat {
     
        private static final int USD = 0;
     
        private static final int JPY = 1;
     
        private static final int CNY = 2;
     
        private static final String US_DOLLAR_SYMBOL = "$";
     
        private static final String JP_YEN_SYMBOL = "\\u00A5"; 
     
        private static final String CN_YUAN_SYMBOL = "\\u00A5"; 
     
        // Object fields
     
        private final NumberFormat formatter; // Number formatter wrapped by this class
        private int currencyType; // Currency type code
        private String currencySymbl; //
        private int fractionDigits;
        private int maxIntDigits;
     
        /** Creates a currency object in the default format.
         * 
         * 
         * 
         * 
         */
        public CurrencyFormat(){
            currencyType = USD;
            currencySymbl = US_DOLLAR_SYMBOL;
            fractionDigits = 2;
     
            maxIntDigits = 0;
            formatter = NumberFormat.getInstance();
            formatter.setRoundingMode(RoundingMode.HALF_UP);
            formatter.setMinimumFractionDigits(fractionDigits);
            formatter.setMaximumFractionDigits(fractionDigits);
            if (maxIntDigits > 0) {
                formatter.setMaximumIntegerDigits (maxIntDigits);
            }
     
        }
     
     
        public static CurrencyFormat getInstance(){
     
            return new CurrencyFormat();
        }
     
     
        public String format (double input) {
            //determin integer digits in number
     
        int intDigits = 0;
        if (input>=1) {
        intDigits =1 +(int) Math.log10(input);
    }
        //determines spaces for currency symbol alignment
     
        int spaces = maxIntDigits - intDigits;
     
    //Use one less alignment space if a zero is front of the decimal place is wanted
     
    if (spaces == maxIntDigits ) {
    	spaces --;
     
    }
     
    //format the number portion.
    input = (Math.round (input * Math.pow(10,fractionDigits))
            / (Math.pow (10.0, fractionDigits)));
    String baseFormat =formatter.format (input);
     
     
    //Start buildning the currency format . Start with the currency Symbol, then add alignment spaces.
     
     
    StringBuilder finalFormat = new StringBuilder (currencySymbl);
    for ( int i =0; i<spaces; i ++) {
    	finalFormat.append(" ");
     
    }
     
    //Check if an additional alignment space is needed for the thousands
    //separator.
    if(maxIntDigits > 3 && intDigits <4) {
    	finalFormat.append(" ");
    }
     
    //Add the formatted number to the right of the currency symbol and alignment spaces.
    finalFormat.append(baseFormat);
     
    return finalFormat.toString();
    }
     
     
        public void setFractionDigits (int fractionDigits){
            formatter.setMinimumFractionDigits(fractionDigits);
            formatter.setMaximumFractionDigits(fractionDigits);
            this.fractionDigits = fractionDigits;
        }
     
     
        public void setInterDigits (int intergerDigits){
            maxIntDigits = intergerDigits;
            formatter.setMaximumIntegerDigits(maxIntDigits);  
        }
     
        public void setCurrency (int currencyCode){
            if (currencyCode == JPY){
                currencyType = JPY;
            currencySymbl = JP_YEN_SYMBOL;
        } else if (currencyCode == CNY){
            currencyType = CNY;
            currencySymbl = CN_YUAN_SYMBOL;
     
        } else if (currencyCode == USD)
            currencyType = USD;
            currencySymbl = US_DOLLAR_SYMBOL;
     
        }              
     
     
        public static void main (String [] args) {
     
            CurrencyFormat currencyFormatter = new CurrencyFormat();
     
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter currency, interger digits, fraction digits, and input"
                    + "number: ");
            String currency = sc.next();
     
            int intergerDigits = sc.nextInt();
            int fractionDigits = sc.nextInt();
            double number = sc.nextDouble();
            switch (currency){
                case "JPY":
                    currencyFormatter.setCurrency(JPY);
                    break;
     
            case "CNY":
            currencyFormatter.setCurrency (CNY);
            break;
            }
     
            currencyFormatter.setInterDigits(intergerDigits);
            currencyFormatter.setFractionDigits(fractionDigits);
            System.out.println("Test Case result " + currencyFormatter.format(number));
     
     
     
     
     
        }
    }

    i should be able to use USD, CNY, and JPY. However it seems only the USD ($) will run... doesnt matter which currency symbol the user requests
    Last edited by champanedout; May 19th, 2014 at 02:30 PM.


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

    Default Re: Need help with Currency Symbol format

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    And thanks for providing a runnable example. Since the user input is unclear, sample runs would also be helpful. Show a few user inputs, actual results, and desired results.

Similar Threads

  1. How to get the overridden currency symbol from the Windows?
    By aworker in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 27th, 2014, 05:17 PM
  2. Replies: 1
    Last Post: June 23rd, 2013, 02:37 AM
  3. I Want To Apply Currency Format To My 2 Dimensional Array?
    By totmore in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 2nd, 2013, 09:29 PM
  4. question about currency symbol
    By darking123 in forum Java Theory & Questions
    Replies: 0
    Last Post: November 18th, 2012, 03:30 AM
  5. Setting what currency i want with getCurrencyInstance()
    By djl1990 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 30th, 2011, 08:54 PM