1 24 package org.ofbiz.base.util; 25 26 import java.text.DateFormat ; 27 import java.text.DecimalFormat ; 28 import java.text.NumberFormat ; 29 import java.text.ParseException ; 30 import java.util.Locale ; 31 import java.util.Currency ; 32 33 41 public class UtilFormatOut { 42 43 public static final String module = UtilFormatOut.class.getName(); 44 45 public static String safeToString(Object obj) { 46 if (obj != null) { 47 return obj.toString(); 48 } else { 49 return ""; 50 } 51 } 52 53 static DecimalFormat priceDecimalFormat = new DecimalFormat ("#,##0.00"); 55 static DecimalFormat priceNumberFormat = new DecimalFormat ("##0.00"); 56 57 61 public static String formatPrice(Double price) { 62 if (price == null) return ""; 63 return formatPrice(price.doubleValue()); 64 } 65 66 70 public static String formatPrice(double price) { 71 return priceDecimalFormat.format(price); 72 } 73 74 public static Double formatPriceNumber(double price) { 75 try { 76 return new Double (priceDecimalFormat.parse(formatPrice(price)).doubleValue()); 77 } catch (ParseException e) { 78 Debug.logError(e, module); 79 return new Double (price); 80 } 81 } 82 83 89 public static String formatCurrency(double price, String isoCode, Locale locale) { 90 com.ibm.icu.text.NumberFormat nf = com.ibm.icu.text.NumberFormat.getCurrencyInstance(locale); 92 if (isoCode != null && isoCode.length() > 1) { 93 nf.setCurrency(com.ibm.icu.util.Currency.getInstance(isoCode)); 94 } else { 95 Debug.logWarning("No isoCode specified to format currency value:" + price, module); 96 } 97 return nf.format(price); 98 } 99 100 106 public static String formatCurrency(Double price, String isoCode, Locale locale) { 107 return formatCurrency(price.doubleValue(), isoCode, locale); 108 } 109 110 115 public static String formatSpelledOutAmount(Double amount, Locale locale) { 116 return formatSpelledOutAmount(amount.doubleValue(), locale); 117 } 118 123 public static String formatSpelledOutAmount(double amount, Locale locale) { 124 com.ibm.icu.text.NumberFormat nf = new com.ibm.icu.text.RuleBasedNumberFormat(locale, com.ibm.icu.text.RuleBasedNumberFormat.SPELLOUT); 126 return nf.format(amount); 127 } 128 129 134 public static String formatAmount(double amount, Locale locale) { 136 com.ibm.icu.text.NumberFormat nf = com.ibm.icu.text.NumberFormat.getInstance(locale); 137 nf.setMinimumFractionDigits(2); 138 nf.setMaximumFractionDigits(2); 139 return nf.format(amount); 140 } 141 142 static DecimalFormat percentageDecimalFormat = new DecimalFormat ("##0.##%"); 144 145 149 public static String formatPercentage(Double percentage) { 150 if (percentage == null) return ""; 151 return formatPercentage(percentage.doubleValue()); 152 } 153 154 158 public static String formatPercentage(double percentage) { 159 return percentageDecimalFormat.format(percentage); 160 } 161 162 static DecimalFormat quantityDecimalFormat = new DecimalFormat ("#,##0.###"); 164 165 169 public static String formatQuantity(Long quantity) { 170 if (quantity == null) 171 return ""; 172 else 173 return formatQuantity(quantity.doubleValue()); 174 } 175 176 180 public static String formatQuantity(long quantity) { 181 return formatQuantity((double) quantity); 182 } 183 184 188 public static String formatQuantity(Integer quantity) { 189 if (quantity == null) 190 return ""; 191 else 192 return formatQuantity(quantity.doubleValue()); 193 } 194 195 199 public static String formatQuantity(int quantity) { 200 return formatQuantity((double) quantity); 201 } 202 203 207 public static String formatQuantity(Float quantity) { 208 if (quantity == null) 209 return ""; 210 else 211 return formatQuantity(quantity.doubleValue()); 212 } 213 214 218 public static String formatQuantity(float quantity) { 219 return formatQuantity((double) quantity); 220 } 221 222 226 public static String formatQuantity(Double quantity) { 227 if (quantity == null) 228 return ""; 229 else 230 return formatQuantity(quantity.doubleValue()); 231 } 232 233 237 public static String formatQuantity(double quantity) { 238 return quantityDecimalFormat.format(quantity); 239 } 240 241 public static String formatPaddedNumber(long number, int numericPadding) { 242 StringBuffer outStrBfr = new StringBuffer (Long.toString(number)); 243 while (numericPadding > outStrBfr.length()) { 244 outStrBfr.insert(0, '0'); 245 } 246 return outStrBfr.toString(); 247 } 248 249 public static String formatPaddingRemove(String original) { 250 if (original == null) return null; 251 StringBuffer orgBuf = new StringBuffer (original); 252 while (orgBuf.length() > 0 && orgBuf.charAt(0) == '0') { 253 orgBuf.deleteCharAt(0); 254 } 255 return orgBuf.toString(); 256 } 257 258 259 264 public static String formatDate(java.sql.Timestamp timestamp) { 265 if (timestamp == null) 266 return ""; 267 DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.FULL); 268 java.util.Date date = (java.util.Date ) timestamp; 269 return df.format(date); 270 } 271 272 277 public static String makeString(Object obj1) { 278 if (obj1 != null) 279 return obj1.toString(); 280 else 281 return ""; 282 } 283 284 288 public static String checkNull(String string1) { 289 if (string1 != null) 290 return string1; 291 else 292 return ""; 293 } 294 295 300 public static String checkNull(String string1, String string2) { 301 if (string1 != null) 302 return string1; 303 else if (string2 != null) 304 return string2; 305 else 306 return ""; 307 } 308 309 315 public static String checkNull(String string1, String string2, String string3) { 316 if (string1 != null) 317 return string1; 318 else if (string2 != null) 319 return string2; 320 else if (string3 != null) 321 return string3; 322 else 323 return ""; 324 } 325 326 333 public static String checkNull(String string1, String string2, String string3, String string4) { 334 if (string1 != null) 335 return string1; 336 else if (string2 != null) 337 return string2; 338 else if (string3 != null) 339 return string3; 340 else if (string4 != null) 341 return string4; 342 else 343 return ""; 344 } 345 346 352 public static String ifNotEmpty(String base, String pre, String post) { 353 if (base != null && base.length() > 0) 354 return pre + base + post; 355 else 356 return ""; 357 } 358 359 364 public static String checkEmpty(String string1, String string2) { 365 if (string1 != null && string1.length() > 0) 366 return string1; 367 else if (string2 != null && string2.length() > 0) 368 return string2; 369 else 370 return ""; 371 } 372 373 379 public static String checkEmpty(String string1, String string2, String string3) { 380 if (string1 != null && string1.length() > 0) 381 return string1; 382 else if (string2 != null && string2.length() > 0) 383 return string2; 384 else if (string3 != null && string3.length() > 0) 385 return string3; 386 else 387 return ""; 388 } 389 390 395 public static String encodeQuery(String query) { 396 String retString; 397 398 retString = replaceString(query, "%", "%25"); 399 retString = replaceString(retString, " ", "%20"); 400 return retString; 401 } 402 403 407 public static String encodeQueryValue(String query) { 408 String retString; 409 410 retString = replaceString(query, "%", "%25"); 411 retString = replaceString(retString, " ", "%20"); 412 retString = replaceString(retString, "&", "%26"); 413 retString = replaceString(retString, "?", "%3F"); 414 retString = replaceString(retString, "=", "%3D"); 415 return retString; 416 } 417 418 424 public static String replaceString(String mainString, String oldString, String newString) { 425 return StringUtil.replaceString(mainString, oldString, newString); 426 } 427 428 432 public static String decodeQueryValue(String query) { 433 String retString; 434 435 retString = replaceString(query, "%25", "%"); 436 retString = replaceString(retString, "%20", " "); 437 retString = replaceString(retString, "%26", "&"); 438 retString = replaceString(retString, "%3F", "?"); 439 retString = replaceString(retString, "%3D", "="); 440 return retString; 441 } 442 443 448 public static String encodeXmlValue(String inString) { 449 String retString = inString; 450 451 retString = StringUtil.replaceString(retString, "&", "&"); 452 retString = StringUtil.replaceString(retString, "<", "<"); 453 retString = StringUtil.replaceString(retString, ">", ">"); 454 retString = StringUtil.replaceString(retString, "\"", """); 455 retString = StringUtil.replaceString(retString, "'", "'"); 456 return retString; 457 } 458 459 public static String padString(String str, int setLen, boolean padEnd, char padChar) { 460 if (str == null) { 461 return null; 462 } 463 if (setLen == 0) { 464 return str; 465 } 466 int stringLen = str.length(); 467 int diff = setLen - stringLen; 468 if (diff < 0) { 469 return str.substring(0, setLen); 470 } else { 471 String newString = new String (); 472 if (padEnd) { 473 newString = newString + str; 474 } 475 for (int i = 0; i < diff; i++) { 476 newString = newString + padChar; 477 } 478 if (!padEnd) { 479 newString = newString + str; 480 } 481 return newString; 482 } 483 } 484 } 485 | Popular Tags |