Code example for DecimalFormat

Methods: setMaximumFractionDigitssetRoundingModeformat

0
        assertEquals("1-useless-api-4", df.format(12345.));
    } 
 
    public void test_setMaximumFractionDigitsAffectsRoundingMode() throws Exception { 
        DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
        df.setMaximumFractionDigits(0);
        df.setRoundingMode(RoundingMode.HALF_UP);
        assertEquals("-0", df.format(-0.2));
        df.setMaximumFractionDigits(1);
        assertEquals("-0.2", df.format(-0.2));
    } 
 
    // Android fails this test, truncating to 127 digits. 
    public void test_setMaximumIntegerDigits() throws Exception { 
        NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
        numberFormat.setGroupingUsed(false);
        numberFormat.setMinimumIntegerDigits(400);
        // The RI's documentation suggests that the int should be formatted to 309 characters -- 
        // a magic number they don't explain -- but the BigInteger should be formatted to the 400 
        // characters we asked for. In practice, the RI uses 309 in both cases. 
        assertEquals(309, numberFormat.format(123).length());