StringLibrary { public static String plainDecimalRepresentation(Number number) { double doubleValue = number.doubleValue(); BigDecimal decimal = BigDecimal.valueOf(doubleValue); if (doubleValue > 1.0 || doubleValue < -1.0) { DecimalFormat decimalFormat = new DecimalFormat("#0.0", new DecimalFormatSymbols(Locale.US)); return decimalFormat.format(decimal); } else { return decimal.toPlainString(); } }  static String unique(String string); static String quoted(String string); static List<String> split(String chainedStrings, Character character); static List<String> split(String chainedStrings, String splittingRegex); static String join(Collection<String> subStrings, Character connector); static String join(Collection<String> subStrings, String connector); static String stripEnd(String string, String suffix); static String firstAfterSplit(String string, Character character); static String firstAfterSplit(String string, String splittingRegex); static String lastAfterSplit(String string, Character character); static String lastAfterSplit(String string, String splittingRegex); static String reversed(String string); static List<String> toStringList(Collection<? extends Object> objects); static List<String> toStringList(Collection<? extends Object> objects, String nullString); static Map<String, String> asStringMap(Map<K, V> sourceMap); static Map<String, V> mapWithStringKeys(Map<K, V> sourceMap); static Map<K, String> mapWithStringValues(Map<K, V> sourceMap); static int maximumToStringLength(Collection<? extends Object> objects, int lengthOfNullToString); static String rightFilled(String string, int targetLength, Character filler); static Collection<String> rightFilled(Collection<String> strings, int targetLength, Character filler); static String leftFilled(String string, int targetLength, Character filler); static Collection<String> leftFilled(Collection<String> strings, int targetLength, Character filler); static String repeated(Character character, int times); static String repeated(String text, int times); static String plainDecimalRepresentation(Number number);  }
@Test public void plainDecimalRepresentationOfNumbers() { assertEquals("0.0", plainDecimalRepresentation(0)); assertEquals("0.0", plainDecimalRepresentation(-0)); assertEquals("0.0", plainDecimalRepresentation(0.0)); assertEquals("0.0", plainDecimalRepresentation(-0.0)); assertEquals("1.0", plainDecimalRepresentation(1)); assertEquals("-1.0", plainDecimalRepresentation(-1)); assertEquals("1.0", plainDecimalRepresentation(1.0)); assertEquals("-1.0", plainDecimalRepresentation(-1.0)); assertEquals("2.0", plainDecimalRepresentation(2)); assertEquals("-2.0", plainDecimalRepresentation(-2)); assertEquals("2.0", plainDecimalRepresentation(2.0)); assertEquals("-2.0", plainDecimalRepresentation(-2.0)); assertEquals("0.05", plainDecimalRepresentation(5.0/100)); assertTrue(plainDecimalRepresentation(1.0/3).startsWith("0.33")); assertEquals("999999999.0", plainDecimalRepresentation(999999999)); assertEquals("-999999999.0", plainDecimalRepresentation(-999999999)); assertEquals("413845470000000000.0", plainDecimalRepresentation(4.1384547E17)); assertEquals("-413845470000000000.0", plainDecimalRepresentation(-4.1384547E17)); assertEquals("0.000000000000000041384547", plainDecimalRepresentation(4.1384547E-17)); assertEquals("-0.000000000000000041384547", plainDecimalRepresentation(-4.1384547E-17)); }
@Test public void decimalRepresentationUsingDot() throws Exception { Locale french = Locale.FRANCE; Double half = NumberFormat.getInstance(french).parse("0,5").doubleValue(); DecimalFormat decimalFormat = new DecimalFormat("#0.0", new DecimalFormatSymbols(french)); assertEquals("0,5", decimalFormat.format(half)); assertEquals("0.5", plainDecimalRepresentation(half)); Double twoAndHalf = NumberFormat.getInstance(french).parse("2,5").doubleValue(); assertEquals("2,5", decimalFormat.format(twoAndHalf)); assertEquals("2.5", plainDecimalRepresentation(twoAndHalf)); }