001    /**
002     * Implements the Cold Fusion Function lscurrencyformat
003     */
004    package railo.runtime.functions.international;
005    
006    import java.text.NumberFormat;
007    import java.util.Currency;
008    import java.util.Locale;
009    
010    import railo.commons.lang.StringUtil;
011    import railo.runtime.PageContext;
012    import railo.runtime.exp.ExpressionException;
013    import railo.runtime.exp.PageException;
014    import railo.runtime.ext.function.Function;
015    import railo.runtime.i18n.LocaleFactory;
016    import railo.runtime.op.Caster;
017    
018    public final class LSCurrencyFormat implements Function {
019            public static String call(PageContext pc , Object number) throws PageException {
020                    return format(toDouble(number), "local", pc.getLocale());
021            }
022            public static String call(PageContext pc , Object number, String type) throws PageException {
023                    return format( toDouble(number), type, pc.getLocale());
024            }
025            public static String call(PageContext pc , Object number, String type,String strLocale) throws PageException {
026                    Locale locale=StringUtil.isEmpty(strLocale)?pc.getLocale():LocaleFactory.getLocale(strLocale);
027                    return format(toDouble(number), type, locale);
028            }
029            
030            public static String format( double number, String type,Locale locale) throws ExpressionException {
031                    type=type.trim().toLowerCase();
032                    if(type.equals("none"))                                 return none(locale,number);
033                    else if(type.equals("local"))                   return local(locale,number);
034                    else if(type.equals("international"))   return international(locale,number);
035                    else {
036                            throw new ExpressionException("invalid type for function lsCurrencyFormat","types are: local, international or none");
037                    }
038                    
039            }
040    
041            public static String none(Locale locale, double number) {
042            NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
043            return StringUtil.replace(nf.format(number),nf.getCurrency().getSymbol(locale),"",false).trim();
044            }
045            
046            public static String local(Locale locale, double number) {
047                    return NumberFormat.getCurrencyInstance(locale).format(number); 
048            }
049            
050            public static String international(Locale locale, double number) {
051            NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
052            Currency currency = nf.getCurrency();
053            
054            String str = StringUtil.replace(
055                                    nf.format(number),
056                                    nf.getCurrency().getSymbol(locale),
057                                    "",false).trim();
058            
059            return currency.getCurrencyCode()+" "+str;
060            
061            /*return StringUtil.replace(
062                                    nf.format(number),
063                                    nf.getCurrency().getSymbol(locale),
064                                    currency.getCurrencyCode(),false).trim();*/
065            
066            }
067            
068            public static double toDouble(Object number) throws PageException {
069                    if(number instanceof String && ((String)number).length()==0) return 0d;
070                    return Caster.toDoubleValue(number);
071            }
072        
073        /*private static String removeCurrencyFromPatterns(String pattern) {
074            return pattern.replace('οΎ€',' ');
075        }*/
076            
077            
078            /*public static void main(String[] args) throws Exception {
079    
080            print.ln(international(Locale.US,123456));
081            print.ln(local(Locale.US,123456));
082            print.ln(none(Locale.US,123456));
083            
084            }*/
085    }