1 // © 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html#License 3 /* 4 ******************************************************************************* 5 * Copyright (C) 2006-2011, International Business Machines Corporation and * 6 * others. All Rights Reserved. * 7 ******************************************************************************* 8 */ 9 10 package com.ibm.icu.tests; 11 12 import java.util.Locale; 13 14 import com.ibm.icu.text.DecimalFormat; 15 import com.ibm.icu.text.DecimalFormatSymbols; 16 17 public class DecimalFormatTest extends ICUTestCase { 18 private static final long lmax = Long.MAX_VALUE; 19 private static final double dsmall = 23.33; 20 21 /* 22 * Test method for 'com.ibm.icu.text.DecimalFormat.clone()' 23 */ testClone()24 public void testClone() { 25 DecimalFormat df = new DecimalFormat("#,#0.00"); 26 DecimalFormat df2 = new DecimalFormat("#,#0.00"); 27 DecimalFormat dfn = new DecimalFormat("#,#0.00"); 28 dfn.setNegativePrefix(dfn.getNegativePrefix() + '!'); 29 testEHCS(df, df2, dfn); 30 } 31 32 /* 33 * Test method for 'com.ibm.icu.text.DecimalFormat.DecimalFormat(DecimalFormat)' 34 */ testDecimalFormatDecimalFormat()35 public void testDecimalFormatDecimalFormat() { 36 // tested implicitly 37 } 38 39 /* 40 * Test method for 'com.ibm.icu.text.DecimalFormat.DecimalFormat()' 41 */ testDecimalFormat()42 public void testDecimalFormat() { 43 DecimalFormat df = new DecimalFormat(); 44 assertEquals("9,223,372,036,854,775,807", df.format(lmax)); 45 } 46 47 /* 48 * Test method for 'com.ibm.icu.text.DecimalFormat.DecimalFormat(String)' 49 */ testDecimalFormatString()50 public void testDecimalFormatString() { 51 DecimalFormat df = new DecimalFormat("#,##0.000"); 52 assertEquals("23.330", df.format(dsmall)); 53 } 54 55 /* 56 * Test method for 'com.ibm.icu.text.DecimalFormat.DecimalFormat(String, DecimalFormatSymbols)' 57 */ testDecimalFormatStringDecimalFormatSymbols()58 public void testDecimalFormatStringDecimalFormatSymbols() { 59 DecimalFormatSymbols sym = new DecimalFormatSymbols(Locale.FRANCE); 60 DecimalFormat df = new DecimalFormat("#,##0.000", sym); 61 assertEquals("23,330", df.format(dsmall)); 62 } 63 64 /* 65 * Test method for 'com.ibm.icu.text.DecimalFormat.getDecimalFormatSymbols()' 66 */ testGetDecimalFormatSymbols()67 public void testGetDecimalFormatSymbols() { 68 DecimalFormatSymbols sym = new DecimalFormatSymbols(Locale.FRANCE); 69 DecimalFormat df = new DecimalFormat("#,##0.000", sym); 70 assertEquals(sym, df.getDecimalFormatSymbols()); 71 } 72 73 /* 74 * Test method for 'com.ibm.icu.text.DecimalFormat.setDecimalFormatSymbols(DecimalFormatSymbols)' 75 */ testSetDecimalFormatSymbols()76 public void testSetDecimalFormatSymbols() { 77 DecimalFormat df = new DecimalFormat(); 78 df.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.FRANCE)); 79 assertEquals("23,33", df.format(dsmall)); 80 } 81 82 /* 83 * Test method for 'com.ibm.icu.text.DecimalFormat.getPositivePrefix()' 84 */ testGetPositivePrefix()85 public void testGetPositivePrefix() { 86 DecimalFormat df = new DecimalFormat("+#,##0.#;-#,##0.#"); 87 assertEquals("+", df.getPositivePrefix()); 88 } 89 90 /* 91 * Test method for 'com.ibm.icu.text.DecimalFormat.setPositivePrefix(String)' 92 */ testSetPositivePrefix()93 public void testSetPositivePrefix() { 94 DecimalFormat df = new DecimalFormat("+#,##0.#;-#,##0.#"); 95 df.setPositivePrefix("?"); 96 assertEquals("?23.3", df.format(dsmall)); 97 } 98 99 /* 100 * Test method for 'com.ibm.icu.text.DecimalFormat.getNegativePrefix()' 101 */ testGetNegativePrefix()102 public void testGetNegativePrefix() { 103 DecimalFormat df = new DecimalFormat("+#,##0.#;-#,##0.#"); 104 assertEquals("-", df.getNegativePrefix()); 105 } 106 107 /* 108 * Test method for 'com.ibm.icu.text.DecimalFormat.setNegativePrefix(String)' 109 */ testSetNegativePrefix()110 public void testSetNegativePrefix() { 111 DecimalFormat df = new DecimalFormat("+#,##0.#;-#,##0.#"); 112 df.setNegativePrefix("~"); 113 assertEquals("~23.3", df.format(-dsmall)); 114 } 115 116 /* 117 * Test method for 'com.ibm.icu.text.DecimalFormat.getPositiveSuffix()' 118 */ testGetPositiveSuffix()119 public void testGetPositiveSuffix() { 120 DecimalFormat df = new DecimalFormat("+#,##0.#**;-#,##0.#~~"); 121 assertEquals("**", df.getPositiveSuffix()); 122 } 123 124 /* 125 * Test method for 'com.ibm.icu.text.DecimalFormat.setPositiveSuffix(String)' 126 */ testSetPositiveSuffix()127 public void testSetPositiveSuffix() { 128 DecimalFormat df = new DecimalFormat("+#,##0.#;-#,##0.#"); 129 df.setPositiveSuffix("**"); 130 assertEquals("+23.3**", df.format(dsmall)); 131 } 132 133 /* 134 * Test method for 'com.ibm.icu.text.DecimalFormat.getNegativeSuffix()' 135 */ testGetNegativeSuffix()136 public void testGetNegativeSuffix() { 137 DecimalFormat df = new DecimalFormat("+#,##0.#**;-#,##0.#~~"); 138 assertEquals("~~", df.getNegativeSuffix()); 139 } 140 141 /* 142 * Test method for 'com.ibm.icu.text.DecimalFormat.setNegativeSuffix(String)' 143 */ testSetNegativeSuffix()144 public void testSetNegativeSuffix() { 145 DecimalFormat df = new DecimalFormat("+#,##0.#;-#,##0.#"); 146 df.setNegativeSuffix("~~"); 147 assertEquals("-23.3~~", df.format(-dsmall)); 148 } 149 150 /* 151 * Test method for 'com.ibm.icu.text.DecimalFormat.getMultiplier()' 152 */ testGetMultiplier()153 public void testGetMultiplier() { 154 DecimalFormat df = new DecimalFormat("%000"); 155 df.setMultiplier(1000); 156 assertEquals(1000, df.getMultiplier()); 157 } 158 159 /* 160 * Test method for 'com.ibm.icu.text.DecimalFormat.setMultiplier(int)' 161 */ testSetMultiplier()162 public void testSetMultiplier() { 163 DecimalFormat df = new DecimalFormat("%000"); 164 assertEquals("%012", df.format(.123)); 165 df.setMultiplier(1000); 166 assertEquals("%123", df.format(.123)); 167 } 168 169 /* 170 * Test method for 'com.ibm.icu.text.DecimalFormat.getGroupingSize()' 171 */ testGetGroupingSize()172 public void testGetGroupingSize() { 173 DecimalFormat df = new DecimalFormat("#,#0.#"); 174 assertEquals(2, df.getGroupingSize()); 175 } 176 177 /* 178 * Test method for 'com.ibm.icu.text.DecimalFormat.setGroupingSize(int)' 179 */ testSetGroupingSize()180 public void testSetGroupingSize() { 181 DecimalFormat df = new DecimalFormat("#,##0.##"); 182 assertEquals("1,234,567.89", df.format(1234567.89)); 183 df.setGroupingSize(2); 184 assertEquals("1,23,45,67.89", df.format(1234567.89)); 185 } 186 187 /* 188 * Test method for 'com.ibm.icu.text.DecimalFormat.isDecimalSeparatorAlwaysShown()' 189 */ testIsDecimalSeparatorAlwaysShown()190 public void testIsDecimalSeparatorAlwaysShown() { 191 DecimalFormat df = new DecimalFormat("#.#"); 192 df.setDecimalSeparatorAlwaysShown(false); 193 assertEquals("1", df.format(1)); 194 assertEquals("1.2", df.format(1.2)); 195 df.setDecimalSeparatorAlwaysShown(true); 196 assertEquals("1.", df.format(1)); 197 } 198 199 /* 200 * Test method for 'com.ibm.icu.text.DecimalFormat.setDecimalSeparatorAlwaysShown(boolean)' 201 */ testSetDecimalSeparatorAlwaysShown()202 public void testSetDecimalSeparatorAlwaysShown() { 203 DecimalFormat df = new DecimalFormat("#.#"); 204 df.setDecimalSeparatorAlwaysShown(false); 205 assertFalse(df.isDecimalSeparatorAlwaysShown()); 206 df.setDecimalSeparatorAlwaysShown(true); 207 assertTrue(df.isDecimalSeparatorAlwaysShown()); 208 } 209 210 /* 211 * Test method for 'com.ibm.icu.text.DecimalFormat.toPattern()' 212 */ testToPattern()213 public void testToPattern() { 214 DecimalFormat df = new DecimalFormat("#,##0.##"); 215 assertEquals("#,##0.##", df.toPattern()); 216 } 217 218 /* 219 * Test method for 'com.ibm.icu.text.DecimalFormat.toLocalizedPattern()' 220 */ testToLocalizedPattern()221 public void testToLocalizedPattern() { 222 DecimalFormat df = new DecimalFormat("#,##0.##", new DecimalFormatSymbols(Locale.FRANCE)); 223 assertEquals("#,##0.##", df.toPattern()); 224 assertEquals("#\u00a0##0,##", df.toLocalizedPattern()); 225 } 226 227 /* 228 * Test method for 'com.ibm.icu.text.DecimalFormat.applyPattern(String)' 229 */ testApplyPattern()230 public void testApplyPattern() { 231 DecimalFormat df = new DecimalFormat("#,##0.##"); 232 df.applyPattern("#,0.#"); 233 assertEquals("1,2,3.4", df.format(123.4)); 234 } 235 236 /* 237 * Test method for 'com.ibm.icu.text.DecimalFormat.applyLocalizedPattern(String)' 238 */ testApplyLocalizedPattern()239 public void testApplyLocalizedPattern() { 240 DecimalFormat df = new DecimalFormat("#,##0.##", new DecimalFormatSymbols(Locale.FRANCE)); 241 df.applyLocalizedPattern("#\u00a00,#"); 242 assertEquals("1\u00a02\u00a03,4", df.format(123.4)); 243 } 244 } 245