1 27 28 package org.ofbiz.base.util; 29 30 import java.math.BigDecimal ; 31 import java.util.HashMap ; 32 import java.util.Locale ; 33 34 import com.ibm.icu.text.RuleBasedNumberFormat; 35 36 public class UtilNumber { 37 38 public static String module = UtilNumber.class.getName(); 39 40 private static final String arithmeticPropertiesFile = "arithmetic.properties"; 42 43 private static final int DEFAULT_BD_SCALE = 2; 45 private static final int DEFAULT_BD_ROUNDING_MODE = BigDecimal.ROUND_HALF_UP; 46 47 53 public static int getBigDecimalScale(String file, String property) { 54 if ((file == null) || (file.length() == 0)) return DEFAULT_BD_SCALE; 55 if ((property == null) || (property.length() == 0)) return DEFAULT_BD_SCALE; 56 57 int scale = -1; 58 String value = UtilProperties.getPropertyValue(file, property); 59 if (value != null) { 60 try { 61 scale = Integer.parseInt(value); 62 } catch (NumberFormatException e) { 63 } 64 } 65 if (scale == -1) { 66 Debug.logWarning("Could not set decimal precision from " + property + "=" + value + ". Using default scale of " + DEFAULT_BD_SCALE + ".", module); 67 scale = DEFAULT_BD_SCALE; 68 } 69 return scale; 70 } 71 72 75 public static int getBigDecimalScale(String property) { 76 return getBigDecimalScale(arithmeticPropertiesFile, property); 77 } 78 79 85 public static int getBigDecimalRoundingMode(String file, String property) { 86 if ((file == null) || (file.length() == 0)) return DEFAULT_BD_SCALE; 87 if ((property == null) || (property.length() == 0)) return DEFAULT_BD_ROUNDING_MODE; 88 89 String value = UtilProperties.getPropertyValue(file, property); 90 int mode = roundingModeFromString(value); 91 if (mode == -1) { 92 Debug.logWarning("Could not set decimal rounding mode from " + property + "=" + value + ". Using default mode of " + DEFAULT_BD_SCALE + ".", module); 93 return DEFAULT_BD_ROUNDING_MODE; 94 } 95 return mode; 96 } 97 98 101 public static int getBigDecimalRoundingMode(String property) { 102 return getBigDecimalRoundingMode(arithmeticPropertiesFile, property); 103 } 104 105 110 public static int roundingModeFromString(String value) { 111 if (value == null) return -1; 112 value = value.trim(); 113 if ("ROUND_HALF_UP".equals(value)) return BigDecimal.ROUND_HALF_UP; 114 else if ("ROUND_HALF_DOWN".equals(value)) return BigDecimal.ROUND_HALF_DOWN; 115 else if ("ROUND_HALF_EVEN".equals(value)) return BigDecimal.ROUND_HALF_EVEN; 116 else if ("ROUND_UP".equals(value)) return BigDecimal.ROUND_UP; 117 else if ("ROUND_DOWN".equals(value)) return BigDecimal.ROUND_DOWN; 118 else if ("ROUND_CEILING".equals(value)) return BigDecimal.ROUND_CEILING; 119 else if ("ROUND_FLOOR".equals(value)) return BigDecimal.ROUND_FLOOR; 120 else if ("ROUND_UNNECCESSARY".equals(value)) return BigDecimal.ROUND_UNNECESSARY; 121 return -1; 122 } 123 124 public static final String ruleSet_en_US = 127 134 "%dollars-and-cents:\n" 135 + " x.0: << [and >%%cents>];\n" 136 + " 0.x: >%%cents>;\n" 137 + " 0: zero dollars; one dollar; =%%main= dollars;\n" 138 + "%%main:\n" 139 + " zero; one; two; three; four; five; six; seven; eight; nine;\n" 140 + " ten; eleven; twelve; thirteen; fourteen; fifteen; sixteen;\n" 141 + " seventeen; eighteen; nineteen;\n" 142 + " 20: twenty[->>];\n" 143 + " 30: thirty[->>];\n" 144 + " 40: forty[->>];\n" 145 + " 50: fifty[->>];\n" 146 + " 60: sixty[->>];\n" 147 + " 70: seventy[->>];\n" 148 + " 80: eighty[->>];\n" 149 + " 90: ninety[->>];\n" 150 + " 100: << hundred[ >>];\n" 151 + " 1000: << thousand[ >>];\n" 152 + " 1,000,000: << million[ >>];\n" 153 + " 1,000,000,000: << billion[ >>];\n" 154 + " 1,000,000,000,000: << trillion[ >>];\n" 155 + " 1,000,000,000,000,000: =#,##0=;\n" 156 + "%%cents:\n" 157 + " 100: <%%main< cent[s];\n" 158 + "%dollars-and-hundredths:\n" 159 + " x.0: <%%main< and >%%hundredths>/100;\n" + "%%hundredths:\n" 161 + " 100: <00<;\n"; 162 163 public static HashMap rbnfRuleSets; 165 static { 166 rbnfRuleSets = new HashMap (); 167 rbnfRuleSets.put(Locale.US, ruleSet_en_US); 168 } 169 170 183 public static String formatRuleBasedAmount(double amount, String rule, Locale locale) { 184 String ruleSet = (String ) rbnfRuleSets.get(locale); 185 if (ruleSet == null) { 186 Debug.logWarning("Cannot format rule based amount for locale " + locale.toString() + " because rule set for that locale does not exist", module); 187 return ""; 188 } 189 RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(ruleSet, locale); 190 String result = ""; 191 try { 192 result = formatter.format(amount, rule); 193 } catch (Exception e) { 194 Debug.logError(e, "Failed to format amount " + amount + " using rule " + rule, module); 195 } 196 return result; 197 } 198 199 207 public static String toPercentString(Number number, int scale, int roundingMode) { 208 if (!(number instanceof BigDecimal )) { 210 number = new BigDecimal (number.doubleValue()); 211 } 212 213 BigDecimal bd = (BigDecimal ) number; 215 216 bd = bd.multiply(new BigDecimal (100.0)).setScale(scale, roundingMode); 218 219 return (bd.toString() + "%"); 220 } 221 } 222 | Popular Tags |