Java Code Examples for java.text.DecimalFormat#getPercentInstance()

The following examples show how to use java.text.DecimalFormat#getPercentInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MilliPctTest.java    From onos with Apache License 2.0 5 votes vote down vote up
@Test
public void testToString() {
    NumberFormat pf = DecimalFormat.getPercentInstance(); //Varies by machine
    pf.setMaximumFractionDigits(3);
    assertEquals(pf.format(0.12345f), MilliPct.ofMilliPct(12345).toString());
    assertEquals(pf.format(-0.12345f), MilliPct.ofMilliPct(-12345).toString());
}
 
Example 2
Source File: Bug8165466.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
    DecimalFormat nf = (DecimalFormat) DecimalFormat
            .getPercentInstance(Locale.US);
    nf.setMaximumFractionDigits(3);
    nf.setMinimumFractionDigits(0);
    nf.setMultiplier(1);

    double d = 0.005678;
    String result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }

    d = 0.00;
    result = nf.format(d);
    if (!result.equals("0%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0%, Found: " + result
                + "]");
    }

    d = 0.005678;
    result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }

    //checking with the non zero value
    d = 0.005678;
    result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }

    d = 9.00;
    result = nf.format(d);
    if (!result.equals("9%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 9%, Found: " + result
                + "]");
    }

    d = 0.005678;
    result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }
}
 
Example 3
Source File: Bug8165466.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
    DecimalFormat nf = (DecimalFormat) DecimalFormat
            .getPercentInstance(Locale.US);
    nf.setMaximumFractionDigits(3);
    nf.setMinimumFractionDigits(0);
    nf.setMultiplier(1);

    double d = 0.005678;
    String result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }

    d = 0.00;
    result = nf.format(d);
    if (!result.equals("0%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0%, Found: " + result
                + "]");
    }

    d = 0.005678;
    result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }

    //checking with the non zero value
    d = 0.005678;
    result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }

    d = 9.00;
    result = nf.format(d);
    if (!result.equals("9%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 9%, Found: " + result
                + "]");
    }

    d = 0.005678;
    result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }
}
 
Example 4
Source File: Bug8165466.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
    DecimalFormat nf = (DecimalFormat) DecimalFormat
            .getPercentInstance(Locale.US);
    nf.setMaximumFractionDigits(3);
    nf.setMinimumFractionDigits(0);
    nf.setMultiplier(1);

    double d = 0.005678;
    String result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }

    d = 0.00;
    result = nf.format(d);
    if (!result.equals("0%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0%, Found: " + result
                + "]");
    }

    d = 0.005678;
    result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }

    //checking with the non zero value
    d = 0.005678;
    result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }

    d = 9.00;
    result = nf.format(d);
    if (!result.equals("9%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 9%, Found: " + result
                + "]");
    }

    d = 0.005678;
    result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }
}
 
Example 5
Source File: Bug8165466.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
    DecimalFormat nf = (DecimalFormat) DecimalFormat
            .getPercentInstance(Locale.US);
    nf.setMaximumFractionDigits(3);
    nf.setMinimumFractionDigits(0);
    nf.setMultiplier(1);

    double d = 0.005678;
    String result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }

    d = 0.00;
    result = nf.format(d);
    if (!result.equals("0%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0%, Found: " + result
                + "]");
    }

    d = 0.005678;
    result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }

    //checking with the non zero value
    d = 0.005678;
    result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }

    d = 9.00;
    result = nf.format(d);
    if (!result.equals("9%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 9%, Found: " + result
                + "]");
    }

    d = 0.005678;
    result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }
}
 
Example 6
Source File: Bug8165466.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
    DecimalFormat nf = (DecimalFormat) DecimalFormat
            .getPercentInstance(Locale.US);
    nf.setMaximumFractionDigits(3);
    nf.setMinimumFractionDigits(0);
    nf.setMultiplier(1);

    double d = 0.005678;
    String result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }

    d = 0.00;
    result = nf.format(d);
    if (!result.equals("0%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0%, Found: " + result
                + "]");
    }

    d = 0.005678;
    result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }

    //checking with the non zero value
    d = 0.005678;
    result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }

    d = 9.00;
    result = nf.format(d);
    if (!result.equals("9%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 9%, Found: " + result
                + "]");
    }

    d = 0.005678;
    result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }
}
 
Example 7
Source File: Bug8165466.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
    DecimalFormat nf = (DecimalFormat) DecimalFormat
            .getPercentInstance(Locale.US);
    nf.setMaximumFractionDigits(3);
    nf.setMinimumFractionDigits(0);
    nf.setMultiplier(1);

    double d = 0.005678;
    String result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }

    d = 0.00;
    result = nf.format(d);
    if (!result.equals("0%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0%, Found: " + result
                + "]");
    }

    d = 0.005678;
    result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }

    //checking with the non zero value
    d = 0.005678;
    result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }

    d = 9.00;
    result = nf.format(d);
    if (!result.equals("9%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 9%, Found: " + result
                + "]");
    }

    d = 0.005678;
    result = nf.format(d);
    if (!result.equals("0.006%")) {
        throw new RuntimeException("[Failed while formatting the double"
                + " value: " + d + " Expected: 0.006%, Found: " + result
                + "]");
    }
}