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 **/ 019package lucee.runtime.chart; 020 021import java.util.Locale; 022 023import lucee.runtime.PageContext; 024import lucee.runtime.engine.ThreadLocalPageContext; 025import lucee.runtime.exp.PageException; 026import lucee.runtime.functions.international.LSCurrencyFormat; 027import lucee.runtime.functions.international.LSDateFormat; 028import lucee.runtime.op.Caster; 029import lucee.runtime.type.dt.DateTime; 030 031public class LabelFormatUtil { 032 033 034 public static final int LABEL_FORMAT_NUMBER = 0; 035 public static final int LABEL_FORMAT_CURRENCY = 1; 036 public static final int LABEL_FORMAT_PERCENT = 2; 037 public static final int LABEL_FORMAT_DATE = 3; 038 039 public static String formatDate(PageContext pc,double value) { 040 DateTime d = Caster.toDate(Caster.toDouble(value),true,null,null); 041 042 try { 043 return LSDateFormat.call(pc, d); 044 } catch (PageException e) { 045 } 046 return Caster.toString(d,null); 047 } 048 049 public static String formatNumber(double value) { 050 return Caster.toString(value); 051 } 052 053 public static String formatPercent(double value) { 054 return Caster.toIntValue(value*100)+" %"; 055 } 056 057 public static String formatCurrency(PageContext pc,double value) { 058 //PageContext pc = Thread LocalPageContext.get(); 059 Locale locale=pc==null?Locale.US:pc.getLocale(); 060 return LSCurrencyFormat.local(locale, value); 061 } 062 063 public static String format(int labelFormat, double value) { 064 065 switch(labelFormat) { 066 case LABEL_FORMAT_CURRENCY: return formatCurrency(ThreadLocalPageContext.get(),value); 067 case LABEL_FORMAT_DATE: return formatDate(ThreadLocalPageContext.get(),value); 068 case LABEL_FORMAT_NUMBER: return formatNumber(value); 069 case LABEL_FORMAT_PERCENT: return formatPercent(value); 070 } 071 return Caster.toString(value); 072 } 073}