1 30 31 34 package com.nightlabs.l10n; 35 36 import java.text.NumberFormat ; 37 import java.text.ParseException ; 38 import java.util.Locale ; 39 40 import com.nightlabs.config.Config; 41 import com.nightlabs.config.ConfigException; 42 43 44 47 public class NumberFormatter 48 { 49 private static NumberFormatter _sharedInstance = null; 50 51 58 public static NumberFormatter sharedInstance() 59 { 60 if (_sharedInstance == null) 61 _sharedInstance = new NumberFormatter(Config.sharedInstance(), Locale.getDefault()); 62 63 return _sharedInstance; 64 } 65 66 protected Config config; 67 protected Locale locale; 68 69 72 public NumberFormatter(Config config, Locale locale) 73 { 74 this.config = config; 75 this.locale = locale; 76 try { 77 config.createConfigModule(GlobalL10nSettings.class); 78 } catch (ConfigException e) { 79 throw new RuntimeException (e); 80 } 81 } 82 83 private NumberFormatProvider numberFormatProvider = null; 84 85 public NumberFormatProvider getNumberFormatProvider() 86 { 87 if (numberFormatProvider != null) 88 return numberFormatProvider; 89 90 L10nFormatCfMod l10nFormatCfMod; 91 try { 92 l10nFormatCfMod = (L10nFormatCfMod) ConfigUtil.createConfigModule( 93 config, L10nFormatCfMod.class, locale.getLanguage(), locale.getCountry()); 94 95 String className = l10nFormatCfMod.getNumberFormatProvider(); 96 97 Class clazz = Class.forName(className); 98 if (!NumberFormatProvider.class.isAssignableFrom(clazz)) 99 throw new ClassCastException ("class \""+className+"\" defined in config module \""+L10nFormatCfMod.class.getName()+"\" does not implement interface \""+NumberFormatProvider.class.getName()+"\"!"); 100 101 NumberFormatProvider nfp = (NumberFormatProvider) clazz.newInstance(); 102 nfp.init(config, locale.getLanguage(), locale.getCountry()); 103 104 this.numberFormatProvider = nfp; 105 return numberFormatProvider; 106 } catch (RuntimeException e) { 107 throw e; 108 } catch (Exception e) { 109 throw new RuntimeException (e); 110 } 111 } 112 113 116 public static String formatInt(long l) 117 { 118 return formatInt(l, 1); 119 } 120 121 125 public static String formatInt(long l, int minIntegerDigitCount) 126 { 127 NumberFormat numberFormat = sharedInstance().getNumberFormatProvider().getIntegerFormat( 128 minIntegerDigitCount); 129 return numberFormat.format(l); 130 } 131 132 136 public static String formatFloat(double d, int decimalDigitCount) 137 { 138 return formatFloat(d, 1, decimalDigitCount, decimalDigitCount); 139 } 140 141 147 public static String formatFloat(double d, int minIntegerDigitCount, int minDecimalDigitCount, int maxDecimalDigitCount) 148 { 149 NumberFormat numberFormat = sharedInstance().getNumberFormatProvider().getFloatFormat( 150 minIntegerDigitCount, minDecimalDigitCount, maxDecimalDigitCount); 151 return numberFormat.format(d); 152 } 153 154 161 public static String formatScientific(double d, int minIntegerDigitCount, int minDecimalDigitCount, int maxDecimalDigitCount, int exponentDigitCount) 162 { 163 NumberFormat numberFormat = sharedInstance().getNumberFormatProvider().getScientificFormat( 164 minIntegerDigitCount, minDecimalDigitCount, maxDecimalDigitCount, exponentDigitCount); 165 return numberFormat.format(d); 166 } 167 168 private static long power(long base, int exponent) { if (exponent < 0) 170 throw new IllegalArgumentException ("exponent < 0!"); 171 172 long result = 1; 173 while (exponent != 0) { 174 if (exponent % 2 != 0) 175 result = result * base; 176 exponent = exponent / 2; 177 base = base * base; 178 } 179 return result; 180 } 181 182 186 public static String formatCurrency( 187 long currencyValue, Currency currency) 188 { 189 return formatCurrency(currencyValue, currency, true); 190 } 191 192 197 public static String formatCurrency( 198 long currencyValue, Currency currency, boolean includeCurrencySymbol) 199 { 200 int ddc = currency.getDecimalDigitCount(); 201 return formatCurrency(currencyValue, 1, ddc, ddc, currency, 202 includeCurrencySymbol); 203 } 204 205 210 public static String formatCurrency( 211 long currencyValue, int minIntegerDigitCount, 212 Currency currency) 213 { 214 return formatCurrency( 215 currencyValue, minIntegerDigitCount, 216 currency); 217 } 218 219 225 public static String formatCurrency( 226 long currencyValue, int minIntegerDigitCount, 227 Currency currency, boolean includeCurrencySymbol) 228 { 229 int ddc = currency.getDecimalDigitCount(); 230 return formatCurrency( 231 currencyValue, 232 minIntegerDigitCount, ddc, ddc, currency, 233 includeCurrencySymbol); 234 } 235 236 243 public static String formatCurrency( 244 long currencyValue, int minIntegerDigitCount, 245 int minDecimalDigitCount, int maxDecimalDigitCount, 246 Currency currency) 247 { 248 return formatCurrency( 249 currencyValue, minIntegerDigitCount, 250 minDecimalDigitCount, maxDecimalDigitCount, 251 currency); 252 } 253 254 262 public static String formatCurrency( 263 long currencyValue, int minIntegerDigitCount, 264 int minDecimalDigitCount, int maxDecimalDigitCount, 265 Currency currency, boolean includeCurrencySymbol) 266 { 267 NumberFormat numberFormat = sharedInstance().getNumberFormatProvider().getCurrencyFormat( 268 minIntegerDigitCount, minDecimalDigitCount, maxDecimalDigitCount, 269 currency.getCurrencySymbol(), includeCurrencySymbol); 270 271 double cvd = (double)currencyValue / power(10, currency.getDecimalDigitCount()); 272 return numberFormat.format(cvd); 273 } 274 275 public static long parseCurrency(String valueStr, Currency currency, boolean includeCurrencySymbol) 276 throws ParseException 277 { 278 NumberFormat numberFormat = sharedInstance().getNumberFormatProvider().getCurrencyFormat( 279 1, currency.getDecimalDigitCount(), currency.getDecimalDigitCount(), 280 currency.getCurrencySymbol(), includeCurrencySymbol); 281 double d = numberFormat.parse(valueStr).doubleValue(); 282 return Math.round(d * power(10, currency.getDecimalDigitCount())); 283 } 284 } 285 | Popular Tags |