/** * @tests java.text.DecimalFormatSymbols#clone() Test of method * java.text.DecimalFormatSymbols#clone(). Case 1: Compare of internal variables of cloned * objects. Case 2: Compare of clones. Case 3: Change the content of the clone. */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "clone", args = {}) public void test_clone() { try { // case 1: Compare of internal variables of cloned objects DecimalFormatSymbols fs = new DecimalFormatSymbols(Locale.US); DecimalFormatSymbols fsc = (DecimalFormatSymbols) fs.clone(); assertEquals(fs.getCurrency(), fsc.getCurrency()); // case 2: Compare of clones fs = new DecimalFormatSymbols(); DecimalFormatSymbols fsc2 = (DecimalFormatSymbols) (fs.clone()); // make sure the objects are equal assertTrue("Object's clone isn't equal!", fs.equals(fsc2)); // case 3: // change the content of the clone and make sure it's not equal // anymore // verifies that it's data is now distinct from the original fs.setNaN("not-a-number"); assertTrue("Object's changed clone should not be equal!", !fs.equals(fsc2)); } catch (Exception e) { fail("Unexpected exception " + e.toString()); } }
/** @tests java.text.DecimalFormatSymbols#setNaN(java.lang.String) */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "setNaN", args = {java.lang.String.class}) public void test_setNaNLjava_lang_String() { dfs.setNaN("NAN!!"); assertEquals("Returned incorrect nan symbol", "NAN!!", dfs.getNaN()); }
/** @tests java.text.DecimalFormatSymbols#getNaN() */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getNaN", args = {}) public void test_getNaN() { dfs.setNaN("NAN!!"); assertEquals("Returned incorrect nan symbol", "NAN!!", dfs.getNaN()); }
/** Constructs a new FormatUtil and initializes various internal formatters. */ public FormatUtil() { this.dateFormat = new SimpleDateFormat("yyyy-MM-dd"); this.integerFormat = NumberFormat.getIntegerInstance(); this.integerFormat.setGroupingUsed(false); this.floatFormat = NumberFormat.getNumberInstance(); this.floatFormat.setGroupingUsed(false); this.floatFormat.setMaximumFractionDigits(6); this.floatFormat.setRoundingMode(RoundingMode.HALF_DOWN); if (this.floatFormat instanceof DecimalFormat) { final DecimalFormat decimalFormat = (DecimalFormat) this.floatFormat; final DecimalFormatSymbols decimalFormatSymbols = decimalFormat.getDecimalFormatSymbols(); decimalFormatSymbols.setNaN("?"); decimalFormatSymbols.setInfinity("?"); decimalFormat.setDecimalFormatSymbols(decimalFormatSymbols); } }
public static String castToString(double d) { double dAbs = Math.abs(d); DecimalFormatSymbols decfmtsymb = new DecimalFormatSymbols( java.util.Locale.US); // US locale represents numbers with a . as comma and without any // (ten)thousend separators. decfmtsymb.setInfinity("INF"); decfmtsymb.setNaN("NaN"); DecimalFormat decfmt = new DecimalFormat( (dAbs > 1E7 || dAbs < 1E-3) && dAbs > 0.0 ? "0.##############E000" : "0.#############", decfmtsymb); String s = decfmt.format(d); // 1.234E567 => 1.234E+567 int e = s.indexOf("E"); if (e > 0 && s.charAt(e + 1) != '-') s = s.substring(0, e) + "E+" + s.substring(e + 1); return s; }