下面列出了java.text.NumberFormat#getPercentInstance ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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();
}
/**
* 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));
}
/**
* 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();
}
/**
* 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();
}
/**
* 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();
}
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);
}
/**
* 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);
}
/**
* 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()));
}
}
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);
}
/**
* Creates an item label generator using default number formatters.
*/
public StandardPieToolTipGenerator() {
this(DEFAULT_SECTION_LABEL_FORMAT, NumberFormat.getNumberInstance(),
NumberFormat.getPercentInstance());
}
protected final String formatZoomText( final double zoom ) {
final NumberFormat numberFormat = NumberFormat.getPercentInstance( swingGuiContext.getLocale() );
return ( numberFormat.format( zoom ) );
}
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());
}