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

The following examples show how to use java.text.NumberFormat#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: Frequency.java    From hipparchus with Apache License 2.0 6 votes vote down vote up
/**
 * Return a string representation of this frequency distribution.
 *
 * @return a string representation.
 */
@Override
public String toString() {
    NumberFormat nf = NumberFormat.getPercentInstance();
    StringBuilder outBuffer = new StringBuilder(200); // this size is just a wild guess
    outBuffer.append("Value \tFreq. \tPct. \tCum Pct. \n");
    Iterator<T> iter = freqTable.keySet().iterator();
    while (iter.hasNext()) {
        T value = iter.next();
        outBuffer.append(value).
                  append('\t').
                  append(getCount(value)).
                  append('\t').
                  append(nf.format(getPct(value))).
                  append('\t').
                  append(nf.format(getCumPct(value))).
                  append('\n');
    }
    return outBuffer.toString();
}
 
Example 2
Source File: Frequency.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Return a string representation of this frequency distribution.
 *
 * @return a string representation.
 */
@Override
public String toString() {
    NumberFormat nf = NumberFormat.getPercentInstance();
    StringBuilder outBuffer = new StringBuilder();
    outBuffer.append("Value \t Freq. \t Pct. \t Cum Pct. \n");
    Iterator<Comparable<?>> iter = freqTable.keySet().iterator();
    while (iter.hasNext()) {
        Comparable<?> value = iter.next();
        outBuffer.append(value);
        outBuffer.append('\t');
        outBuffer.append(getCount(value));
        outBuffer.append('\t');
        outBuffer.append(nf.format(getPct(value)));
        outBuffer.append('\t');
        outBuffer.append(nf.format(getCumPct(value)));
        outBuffer.append('\n');
    }
    return outBuffer.toString();
}
 
Example 3
Source File: Frequency.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Return a string representation of this frequency
 * distribution.
 * 
 * @return a string representation.
 */
public String toString() {
    NumberFormat nf = NumberFormat.getPercentInstance();
    StringBuffer outBuffer = new StringBuffer();
    outBuffer.append("Value \t Freq. \t Pct. \t Cum Pct. \n");
    Iterator iter = freqTable.keySet().iterator();
    while (iter.hasNext()) {
        Object value = iter.next();
        outBuffer.append(value);
        outBuffer.append('\t');
        outBuffer.append(getCount(value));
        outBuffer.append('\t');
        outBuffer.append(nf.format(getPct(value)));
        outBuffer.append('\t');
        outBuffer.append(nf.format(getCumPct(value)));
        outBuffer.append('\n');
    }
    return outBuffer.toString();
}
 
Example 4
Source File: DrawerFragment.java    From syncthing-android with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Populates views with status received via {@link RestApi#getSystemInfo}.
 */
private void onReceiveSystemInfo(SystemInfo info) {
    if (getActivity() == null)
        return;
    NumberFormat percentFormat = NumberFormat.getPercentInstance();
    percentFormat.setMaximumFractionDigits(2);
    mCpuUsage.setText(percentFormat.format(info.cpuPercent / 100));
    mRamUsage.setText(Util.readableFileSize(mActivity, info.sys));
    int announceTotal = info.discoveryMethods;
    int announceConnected =
            announceTotal - Optional.fromNullable(info.discoveryErrors).transform(Map::size).or(0);
    mAnnounceServer.setText(String.format(Locale.getDefault(), "%1$d/%2$d",
                                          announceConnected, announceTotal));
    int color = (announceConnected > 0)
            ? R.color.text_green
            : R.color.text_red;
    mAnnounceServer.setTextColor(ContextCompat.getColor(getContext(), color));
}
 
Example 5
Source File: Math_90_Frequency_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Return a string representation of this frequency
 * distribution.
 * 
 * @return a string representation.
 */
@Override
public String toString() {
    NumberFormat nf = NumberFormat.getPercentInstance();
    StringBuffer outBuffer = new StringBuffer();
    outBuffer.append("Value \t Freq. \t Pct. \t Cum Pct. \n");
    Iterator iter = freqTable.keySet().iterator();
    while (iter.hasNext()) {
        Object value = iter.next();
        outBuffer.append(value);
        outBuffer.append('\t');
        outBuffer.append(getCount(value));
        outBuffer.append('\t');
        outBuffer.append(nf.format(getPct(value)));
        outBuffer.append('\t');
        outBuffer.append(nf.format(getCumPct(value)));
        outBuffer.append('\n');
    }
    return outBuffer.toString();
}
 
Example 6
Source File: DeviceUtils.java    From Aurora with Apache License 2.0 5 votes vote down vote up
public static String percent(double p1, double p2) {
    String str;
    double p3 = p1 / p2;
    NumberFormat nf = NumberFormat.getPercentInstance();
    nf.setMinimumFractionDigits(2);
    str = nf.format(p3);
    return str;
}
 
Example 7
Source File: JProgressBar.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a {@code String} representation of the current progress.
 * By default, this returns a simple percentage {@code String} based on
 * the value returned from {@code getPercentComplete}.  An example
 * would be the "42%".  You can change this by calling {@code setString}.
 *
 * @return the value of the progress string, or a simple percentage string
 *         if the progress string is {@code null}
 * @see    #setString
 */
public String getString(){
    if (progressString != null) {
        return progressString;
    } else {
        if (format == null) {
            format = NumberFormat.getPercentInstance();
        }
        return format.format(new Double(getPercentComplete()));
    }
}
 
Example 8
Source File: BpmnJsReport.java    From camunda-bpm-process-test-coverage with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieve a formated percentage.
 * 
 * @param coverage
 * @return
 */
private static String getCoveragePercent(double coverage) {

    NumberFormat percentFormat = NumberFormat.getPercentInstance();
    percentFormat.setMaximumFractionDigits(1);

    return percentFormat.format(coverage);
}
 
Example 9
Source File: RecipeHandlerSludgeBoiler.java    From NEI-Integration with MIT License 5 votes vote down vote up
@Override
public void drawExtras(int recipe) {
    this.drawProgressBar(129, 0, 176, 58, 8, 62, 1.0F, 3);
    this.drawProgressBar(139, 0, 185, 58, 8, 62, 40, 3);
    
    CachedSludgeBoilerRecipe crecipe = (CachedSludgeBoilerRecipe) this.arecipes.get(recipe);
    NumberFormat percentFormat = NumberFormat.getPercentInstance();
    percentFormat.setMaximumFractionDigits(2);
    GuiDraw.drawStringC(percentFormat.format(crecipe.chance), 57, 44, 0x808080, false);
}
 
Example 10
Source File: StandardPieToolTipGeneratorTest.java    From ccu-historian with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Test that the equals() method distinguishes all fields.
 */
@Test
public void testEquals() {
    StandardPieToolTipGenerator g1 = new StandardPieToolTipGenerator();
    StandardPieToolTipGenerator g2 = new StandardPieToolTipGenerator();
    assertTrue(g1.equals(g2));
    assertTrue(g2.equals(g1));

    g1 = new StandardPieToolTipGenerator("{0}",
            new DecimalFormat("#,##0.00"),
            NumberFormat.getPercentInstance());
    assertFalse(g1.equals(g2));
    g2 = new StandardPieToolTipGenerator("{0}",
            new DecimalFormat("#,##0.00"),
            NumberFormat.getPercentInstance());
    assertTrue(g1.equals(g2));

    g1 = new StandardPieToolTipGenerator("{0} {1}",
            new DecimalFormat("#,##0.00"),
            NumberFormat.getPercentInstance());
    assertFalse(g1.equals(g2));
    g2 = new StandardPieToolTipGenerator("{0} {1}",
            new DecimalFormat("#,##0.00"),
            NumberFormat.getPercentInstance());
    assertTrue(g1.equals(g2));

    g1 = new StandardPieToolTipGenerator("{0} {1}",
            new DecimalFormat("#,##0"), NumberFormat.getPercentInstance());
    assertFalse(g1.equals(g2));
    g2 = new StandardPieToolTipGenerator("{0} {1}",
            new DecimalFormat("#,##0"), NumberFormat.getPercentInstance());
    assertTrue(g1.equals(g2));

    g1 = new StandardPieToolTipGenerator("{0} {1}",
            new DecimalFormat("#,##0"), new DecimalFormat("0.000%"));
    assertFalse(g1.equals(g2));
    g2 = new StandardPieToolTipGenerator("{0} {1}",
            new DecimalFormat("#,##0"), new DecimalFormat("0.000%"));
    assertTrue(g1.equals(g2));
}
 
Example 11
Source File: CPUTestCase.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new instance of CPUTestCase
 */
public CPUTestCase(String name) {
    super(name);
    percentFormat = NumberFormat.getPercentInstance();
    percentFormat.setMaximumFractionDigits(1);
    percentFormat.setMinimumFractionDigits(0);
}
 
Example 12
Source File: DataReductionProxySettings.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
/**
 * @return The data reduction settings as a string percentage.
 */
public String getContentLengthPercentSavings() {
    ContentLengths length = getContentLengths();

    double savings = 0;
    if (length.getOriginal() > 0L  && length.getOriginal() > length.getReceived()) {
        savings = (length.getOriginal() - length.getReceived()) / (double) length.getOriginal();
    }
    NumberFormat percentageFormatter = NumberFormat.getPercentInstance(Locale.getDefault());
    return percentageFormatter.format(savings);
}
 
Example 13
Source File: AbstractCategoryItemLabelGenerator.java    From ccu-historian with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates a label generator with the specified date formatter.
 *
 * @param labelFormat  the label format string (<code>null</code> not
 *                     permitted).
 * @param formatter  the date formatter (<code>null</code> not permitted).
 */
protected AbstractCategoryItemLabelGenerator(String labelFormat,
        DateFormat formatter) {
    ParamChecks.nullNotPermitted(labelFormat, "labelFormat");
    ParamChecks.nullNotPermitted(formatter, "formatter");
    this.labelFormat = labelFormat;
    this.numberFormat = null;
    this.percentFormat = NumberFormat.getPercentInstance();
    this.dateFormat = formatter;
    this.nullValueString = "-";
}
 
Example 14
Source File: StandardPieToolTipGenerator.java    From opensim-gui with Apache License 2.0 4 votes vote down vote up
/**
 * Creates an item label generator using default number formatters.
 */
public StandardPieToolTipGenerator() {
    this(DEFAULT_SECTION_LABEL_FORMAT, NumberFormat.getNumberInstance(), 
            NumberFormat.getPercentInstance());
}
 
Example 15
Source File: PreviewPane.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected final String formatZoomText( final double zoom ) {
  final NumberFormat numberFormat = NumberFormat.getPercentInstance( swingGuiContext.getLocale() );
  return ( numberFormat.format( zoom ) );
}
 
Example 16
Source File: MessageFormatTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public void test_setFormatsByArgumentIndex$Ljava_text_Format() {
    MessageFormat f1 = (MessageFormat) format1.clone();

    // test with repeating formats and max argument index < max offset
    // compare getFormatsByArgumentIndex() results after calls to
    // setFormatsByArgumentIndex(Format[])
    Format[] correctFormats = new Format[] { DateFormat.getTimeInstance(),
            new ChoiceFormat("0#off|1#on"), DateFormat.getTimeInstance(),
            NumberFormat.getCurrencyInstance(),
            new ChoiceFormat("1#few|2#ok|3#a lot") };

    f1.setFormatsByArgumentIndex(correctFormats);
    Format[] formats = f1.getFormatsByArgumentIndex();

    assertEquals("Test1A:Returned wrong number of formats:",
            correctFormats.length, formats.length);
    for (int i = 0; i < correctFormats.length; i++) {
        assertEquals("Test1B:wrong format for argument index " + i + ":",
                correctFormats[i], formats[i]);
    }

    // compare getFormats() results after calls to
    // setFormatByArgumentIndex()
    formats = f1.getFormats();
    correctFormats = new Format[] { NumberFormat.getCurrencyInstance(),
            DateFormat.getTimeInstance(), DateFormat.getTimeInstance(),
            new ChoiceFormat("1#few|2#ok|3#a lot"),
            new ChoiceFormat("0#off|1#on"), DateFormat.getTimeInstance(), };

    assertEquals("Test1C:Returned wrong number of formats:",
            correctFormats.length, formats.length);
    for (int i = 0; i < correctFormats.length; i++) {
        assertEquals("Test1D:wrong format for pattern index " + i + ":",
                correctFormats[i], formats[i]);
    }

    // test setting argumentIndexes that are not used
    MessageFormat f2 = (MessageFormat) format2.clone();
    Format[] inputFormats = new Format[] { DateFormat.getDateInstance(),
            new ChoiceFormat("0#off|1#on"),
            NumberFormat.getPercentInstance(),
            NumberFormat.getCurrencyInstance(),
            DateFormat.getTimeInstance(), null, null, null,
            DateFormat.getTimeInstance() };
    f2.setFormatsByArgumentIndex(inputFormats);

    formats = f2.getFormatsByArgumentIndex();
    correctFormats = format2.getFormatsByArgumentIndex();

    assertEquals("Test2A:Returned wrong number of formats:",
            correctFormats.length, formats.length);
    for (int i = 0; i < correctFormats.length; i++) {
        assertEquals("Test2B:wrong format for argument index " + i + ":",
                correctFormats[i], formats[i]);
    }

    formats = f2.getFormats();
    correctFormats = new Format[] { NumberFormat.getCurrencyInstance(),
            DateFormat.getTimeInstance(), DateFormat.getDateInstance(),
            null, new ChoiceFormat("0#off|1#on"),
            DateFormat.getDateInstance() };

    assertEquals("Test2C:Returned wrong number of formats:",
            correctFormats.length, formats.length);
    for (int i = 0; i < correctFormats.length; i++) {
        assertEquals("Test2D:wrong format for pattern index " + i + ":",
                correctFormats[i], formats[i]);
    }

    // test exceeding the argumentIndex number
    MessageFormat f3 = (MessageFormat) format3.clone();
    f3.setFormatsByArgumentIndex(inputFormats);

    formats = f3.getFormatsByArgumentIndex();
    assertEquals("Test3A:Returned wrong number of formats:", 0,
            formats.length);

    formats = f3.getFormats();
    assertEquals("Test3B:Returned wrong number of formats:", 0,
            formats.length);

}
 
Example 17
Source File: Test6462562.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
void testPercentFormat() {
    NumberFormat format = NumberFormat.getPercentInstance(Locale.US);
    TestFormattedTextField ftf = create(format);
    ftf.setValue(.34);

    System.err.println("Testing NumberFormat.getPercentInstance(Locale.US)");

    // test inserting individual characters
    ftf.test(0, 0, "1", .14);
    ftf.test(2, 0, "2", 1.42);
    ftf.test(1, 0, "0", 1.02);

    // test inserting several characters at once - e.g. from clipboard
    ftf.test(0, 0, "1024", 10.24);
    ftf.test(3, 0, "333", 103.33);
    ftf.test(6, 0, "77", 10333.77);
    ftf.test(4, 0, "99", 10399.77);
    ftf.test(6, 0, "00", 10390.07);

    // test inserting strings that contain some formatting
    ftf.test(0, 0, "2,2", 2290.07);
    ftf.test(2, 0, "2,2", 222.27);
    ftf.test(4, 0, "2,2", 222.22);
    ftf.test(6, 0, "33,33", 2222233.33);

    // test delete
    ftf.test(0, 0, DELETE, 222233.33);
    ftf.test(10, 0, DELETE, 222233.33);
    ftf.test(5, 0, DELETE, 22223.33);
    ftf.test(6, 0, DELETE, 2222.33);

    // test backspace
    ftf.test(0, 0, BACKSPACE, 2222.33);
    ftf.test(7, 0, BACKSPACE, 222.23);
    ftf.test(4, 0, BACKSPACE, 22.23);
    ftf.test(2, 0, BACKSPACE, 2.23);

    // test replacing selection
    ftf.test(0, 1, "555", 555.23);
    ftf.test(4, 2, "555", 5555.55);
    ftf.test(2, 3, "1", 551.55);
    ftf.test(3, 2, "6", 55.65);
    ftf.test(4, 2, "12", 556.12);
    ftf.test(3, 4, "0", 5.5);
    ftf.test(0, 3, "111222333444555", 1112223334445.55);

    // test deleting selection
    ftf.test(0, 2, DELETE, 12223334445.55);
    ftf.test(0, 3, BACKSPACE, 223334445.55);
    ftf.test(12, 2, DELETE, 2233344.45);
    ftf.test(9, 2, BACKSPACE, 22333.44);
    ftf.test(4, 3, DELETE, 223.44);
    ftf.test(1, 2, BACKSPACE, 23.44);
    ftf.test(3, 3, DELETE, .23);
    ftf.test(1, 2, BACKSPACE, .02);
}
 
Example 18
Source File: StandardPieSectionLabelGenerator.java    From buffer_bci with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Creates a new instance for the specified locale.
 *
 * @param labelFormat  the label format (<code>null</code> not permitted).
 * @param locale  the local (<code>null</code> not permitted).
 *
 * @since 1.0.7
 */
public StandardPieSectionLabelGenerator(String labelFormat, Locale locale) {
    this(labelFormat, NumberFormat.getNumberInstance(locale),
            NumberFormat.getPercentInstance(locale));
}
 
Example 19
Source File: StandardPieSectionLabelGenerator.java    From openstock with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Creates a new section label generator using the specified label format
 * string, and platform default number and percentage formatters.
 *
 * @param labelFormat  the label format (<code>null</code> not permitted).
 */
public StandardPieSectionLabelGenerator(String labelFormat) {
    this(labelFormat, NumberFormat.getNumberInstance(),
            NumberFormat.getPercentInstance());
}
 
Example 20
Source File: AbstractCategoryItemLabelGenerator.java    From buffer_bci with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Creates a label generator with the specified number formatter.
 *
 * @param labelFormat  the label format string (<code>null</code> not
 *                     permitted).
 * @param formatter  the number formatter (<code>null</code> not permitted).
 */
protected AbstractCategoryItemLabelGenerator(String labelFormat,
                                             NumberFormat formatter) {
    this(labelFormat, formatter, NumberFormat.getPercentInstance());
}