1 16 17 package org.apache.velocity.tools.generic; 18 19 import java.text.DecimalFormat ; 20 import java.text.DecimalFormatSymbols ; 21 import java.text.NumberFormat ; 22 import java.util.Locale ; 23 24 55 public class NumberTool 56 { 57 58 61 public static final String DEFAULT_FORMAT = "default"; 62 63 private static final int STYLE_NUMBER = 0; 64 private static final int STYLE_CURRENCY = 1; 65 private static final int STYLE_PERCENT = 2; 66 private static final int STYLE_INTEGER = 4; 68 69 72 public NumberTool() 73 { 74 } 76 77 78 80 88 public Locale getLocale() 89 { 90 return Locale.getDefault(); 91 } 92 93 103 public String getFormat() 104 { 105 return DEFAULT_FORMAT; 106 } 107 108 109 111 119 public String format(Object obj) 120 { 121 return format(getFormat(), obj); 122 } 123 124 135 public String format(String format, Object obj) 136 { 137 return format(format, obj, getLocale()); 138 } 139 140 151 public String format(String format, Object obj, Locale locale) 152 { 153 Number number = toNumber(obj); 154 NumberFormat nf = getNumberFormat(format, locale); 155 if (number == null || nf == null) 156 { 157 return null; 158 } 159 return nf.format(number); 160 } 161 162 164 177 public NumberFormat getNumberFormat(String format, Locale locale) 178 { 179 if (format == null) 180 { 181 return null; 182 } 183 184 NumberFormat nf = null; 185 int style = getStyleAsInt(format); 186 if (style < 0) 187 { 188 nf = new DecimalFormat (format, new DecimalFormatSymbols (locale)); 190 } 191 else 192 { 193 nf = getNumberFormat(style, locale); 195 } 196 return nf; 197 } 198 199 210 protected NumberFormat getNumberFormat(int numberStyle, Locale locale) 211 { 212 try 213 { 214 NumberFormat nf; 215 switch (numberStyle) 216 { 217 case STYLE_NUMBER: 218 nf = NumberFormat.getNumberInstance(locale); 219 break; 220 case STYLE_CURRENCY: 221 nf = NumberFormat.getCurrencyInstance(locale); 222 break; 223 case STYLE_PERCENT: 224 nf = NumberFormat.getPercentInstance(locale); 225 break; 226 case STYLE_INTEGER: 227 nf = getIntegerInstance(locale); 228 break; 229 default: 230 nf = null; 232 } 233 return nf; 234 } 235 catch (Exception suppressed) 236 { 237 return null; 239 } 240 } 241 242 250 private NumberFormat getIntegerInstance(Locale locale) 251 { 252 DecimalFormat format = 253 (DecimalFormat )NumberFormat.getNumberInstance(locale); 254 format.setMaximumFractionDigits(0); 255 format.setDecimalSeparatorAlwaysShown(false); 256 format.setParseIntegerOnly(true); 257 return format; 258 } 259 260 271 protected int getStyleAsInt(String style) 272 { 273 if (style == null || style.length() < 6 || style.length() > 8) { 275 return -1; 276 } 277 if (style.equalsIgnoreCase("default")) 278 { 279 return STYLE_NUMBER; 282 } 283 if (style.equalsIgnoreCase("number")) 284 { 285 return STYLE_NUMBER; 286 } 287 if (style.equalsIgnoreCase("currency")) 288 { 289 return STYLE_CURRENCY; 290 } 291 if (style.equalsIgnoreCase("percent")) 292 { 293 return STYLE_PERCENT; 294 } 295 if (style.equalsIgnoreCase("integer")) 296 { 297 return STYLE_INTEGER; 298 } 299 return -1; 301 } 302 303 304 306 316 public Number toNumber(Object obj) 317 { 318 return toNumber(getFormat(), obj, getLocale()); 319 } 320 321 333 public Number toNumber(String format, Object obj) 334 { 335 return toNumber(format, obj, getLocale()); 336 } 337 338 350 public Number toNumber(String format, Object obj, Locale locale) 351 { 352 if (obj == null) 353 { 354 return null; 355 } 356 if (obj instanceof Number ) 357 { 358 return (Number )obj; 359 } 360 try 361 { 362 NumberFormat parser = getNumberFormat(format, locale); 363 return parser.parse(String.valueOf(obj)); 364 } 365 catch (Exception e) 366 { 367 return null; 368 } 369 } 370 371 } 372 | Popular Tags |