Results 1 to 2 of 2
  1. #1
    champaned_out is offline Member
    Join Date
    May 2014
    Posts
    1
    Rep Power
    0

    Default Need help with Currency Symbol format

    Java Code:
    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 print... doesnt matter which currency symbol the user requests, only the $ will be reprinted

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Need help with Currency Symbol format

    Any particular reason you are doing this and not using something like:
    Java Code:
    NumberFormat f = NumberFormat.getCurrencyInstance(Locale.FRANCE);
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 1
    Last Post: 01-28-2014, 04:43 AM
  2. Replies: 12
    Last Post: 05-25-2013, 08:41 PM
  3. Replies: 0
    Last Post: 02-09-2012, 09:25 AM
  4. Replies: 9
    Last Post: 10-18-2008, 08:26 PM
  5. Replies: 3
    Last Post: 05-01-2008, 08:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •