java.text.NumberFormat#getPercentInstance ( )源码实例Demo

下面列出了java.text.NumberFormat#getPercentInstance ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: astor   文件: Frequency.java
/**
 * 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();
}
 
源代码2 项目: syncthing-android   文件: DrawerFragment.java
/**
 * 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));
}
 
源代码3 项目: coming   文件: Math_90_Frequency_t.java
/**
 * 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();
}
 
源代码4 项目: astor   文件: Frequency.java
/**
 * 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();
}
 
源代码5 项目: hipparchus   文件: Frequency.java
/**
 * 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();
}
 
源代码6 项目: Aurora   文件: DeviceUtils.java
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;
}
 
/**
 * 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 = "-";
}
 
/**
 * @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);
}
 
源代码9 项目: netbeans   文件: CPUTestCase.java
/**
 * Creates a new instance of CPUTestCase
 */
public CPUTestCase(String name) {
    super(name);
    percentFormat = NumberFormat.getPercentInstance();
    percentFormat.setMaximumFractionDigits(1);
    percentFormat.setMinimumFractionDigits(0);
}
 
/**
 * 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));
}
 
@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);
}
 
/**
 * Retrieve a formated percentage.
 * 
 * @param coverage
 * @return
 */
private static String getCoveragePercent(double coverage) {

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

    return percentFormat.format(coverage);
}
 
源代码13 项目: openjdk-jdk8u-backup   文件: JProgressBar.java
/**
 * 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()));
    }
}
 
源代码14 项目: j2objc   文件: MessageFormatTest.java
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);

}
 
源代码15 项目: opensim-gui   文件: StandardPieToolTipGenerator.java
/**
 * Creates an item label generator using default number formatters.
 */
public StandardPieToolTipGenerator() {
    this(DEFAULT_SECTION_LABEL_FORMAT, NumberFormat.getNumberInstance(), 
            NumberFormat.getPercentInstance());
}
 
源代码16 项目: pentaho-reporting   文件: PreviewPane.java
protected final String formatZoomText( final double zoom ) {
  final NumberFormat numberFormat = NumberFormat.getPercentInstance( swingGuiContext.getLocale() );
  return ( numberFormat.format( zoom ) );
}
 
源代码17 项目: openjdk-jdk8u   文件: Test6462562.java
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);
}
 
/**
 * 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());
}
 
/**
 * 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));
}
 
/**
 * 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());
}