001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019/**
020 * Implements the CFML Function lscurrencyformat
021 */
022package lucee.runtime.functions.international;
023
024import java.text.NumberFormat;
025import java.util.Currency;
026import java.util.Locale;
027
028import lucee.commons.lang.StringUtil;
029import lucee.runtime.PageContext;
030import lucee.runtime.exp.ExpressionException;
031import lucee.runtime.exp.PageException;
032import lucee.runtime.ext.function.Function;
033import lucee.runtime.i18n.LocaleFactory;
034import lucee.runtime.op.Caster;
035
036public final class LSCurrencyFormat implements Function {
037        public static String call(PageContext pc , Object number) throws PageException {
038                return format(toDouble(number), "local", pc.getLocale());
039        }
040        public static String call(PageContext pc , Object number, String type) throws PageException {
041                return format( toDouble(number), type, pc.getLocale());
042        }
043        public static String call(PageContext pc , Object number, String type,String strLocale) throws PageException {
044                Locale locale=StringUtil.isEmpty(strLocale)?pc.getLocale():LocaleFactory.getLocale(strLocale);
045                return format(toDouble(number), type, locale);
046        }
047        
048        public static String format( double number, String type,Locale locale) throws ExpressionException {
049                type=type.trim().toLowerCase();
050                if(type.equals("none"))                                 return none(locale,number);
051                else if(type.equals("local"))                   return local(locale,number);
052                else if(type.equals("international"))   return international(locale,number);
053                else {
054                        throw new ExpressionException("invalid type for function lsCurrencyFormat","types are: local, international or none");
055                }
056                
057        }
058
059        public static String none(Locale locale, double number) {
060        NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
061        return StringUtil.replace(nf.format(number),nf.getCurrency().getSymbol(locale),"",false).trim();
062        }
063        
064        public static String local(Locale locale, double number) {
065                return NumberFormat.getCurrencyInstance(locale).format(number); 
066        }
067        
068        public static String international(Locale locale, double number) {
069        NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
070        Currency currency = nf.getCurrency();
071        
072        String str = StringUtil.replace(
073                                nf.format(number),
074                                nf.getCurrency().getSymbol(locale),
075                                "",false).trim();
076        
077        return currency.getCurrencyCode()+" "+str;
078        
079        /*return StringUtil.replace(
080                                nf.format(number),
081                                nf.getCurrency().getSymbol(locale),
082                                currency.getCurrencyCode(),false).trim();*/
083        
084        }
085        
086        public static double toDouble(Object number) throws PageException {
087                if(number instanceof String && ((String)number).length()==0) return 0d;
088                return Caster.toDoubleValue(number);
089        }
090}