下面列出了java.text.DecimalFormat#setMultiplier ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void printExtendOsInfo(CommandSender sender) {
NativeManager nativeData = plugin.getNativeData();
//cpu
double systemCpuLoad = nativeData.getCPULoad();
double processCpuLoad = nativeData.getProcessCPULoad();
//these numbers are in percent (0.01 -> 1%)
//we want to to have four places in a human readable percent value to multiple it with 100
DecimalFormat decimalFormat = new DecimalFormat("###.#### %");
decimalFormat.setMultiplier(100);
String systemLoadFormat = decimalFormat.format(systemCpuLoad);
String processLoadFormat = decimalFormat.format(processCpuLoad);
sendMessage(sender,"System Usage", systemLoadFormat);
sendMessage(sender,"Process Usage", processLoadFormat);
//swap
long totalSwap = nativeData.getTotalSwap();
long freeSwap = nativeData.getFreeSwap();
sendMessage(sender, "Total Swap", readableBytes(totalSwap));
sendMessage(sender, "Free Swap", readableBytes(freeSwap));
//RAM
long totalMemory = nativeData.getTotalMemory();
long freeMemory = nativeData.getFreeMemory();
sendMessage(sender, "Total OS RAM", readableBytes(totalMemory));
sendMessage(sender, "Free OS RAM", readableBytes(freeMemory));
}
private void printExtendOsInfo(CommandSender sender) {
NativeManager nativeData = plugin.getNativeData();
//cpu
double systemCpuLoad = nativeData.getCPULoad();
double processCpuLoad = nativeData.getProcessCPULoad();
//these numbers are in percent (0.01 -> 1%)
//we want to to have four places in a human readable percent value to multiple it with 100
DecimalFormat decimalFormat = new DecimalFormat("###.#### %");
decimalFormat.setMultiplier(100);
String systemLoadFormat = decimalFormat.format(systemCpuLoad);
String processLoadFormat = decimalFormat.format(processCpuLoad);
sendMessage(sender,"System Usage", systemLoadFormat);
sendMessage(sender,"Process Usage", processLoadFormat);
//swap
long totalSwap = nativeData.getTotalSwap();
long freeSwap = nativeData.getFreeSwap();
sendMessage(sender, "Total Swap", readableBytes(totalSwap));
sendMessage(sender, "Free Swap", readableBytes(freeSwap));
//RAM
long totalMemory = nativeData.getTotalMemory();
long freeMemory = nativeData.getFreeMemory();
sendMessage(sender, "Total OS RAM", readableBytes(totalMemory));
sendMessage(sender, "Free OS RAM", readableBytes(freeMemory));
}
public void testBigDecimalICUConsistency() {
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance();
df.setMaximumFractionDigits(2);
df.setMultiplier(2);
assertEquals(df.format(BigDecimal.valueOf(0.16)),
df.format(BigDecimal.valueOf(0.16).doubleValue()));
assertEquals(df.format(BigDecimal.valueOf(0.0293)),
df.format(BigDecimal.valueOf(0.0293).doubleValue()));
assertEquals(df.format(BigDecimal.valueOf(0.006)),
df.format(BigDecimal.valueOf(0.006).doubleValue()));
assertEquals(df.format(BigDecimal.valueOf(0.00283)),
df.format(BigDecimal.valueOf(0.00283).doubleValue()));
assertEquals(df.format(BigDecimal.valueOf(1.60)),
df.format(BigDecimal.valueOf(1.60).doubleValue()));
assertEquals(df.format(BigDecimal.valueOf(15)),
df.format(BigDecimal.valueOf(15).doubleValue()));
assertEquals(df.format(BigDecimal.valueOf(170)),
df.format(BigDecimal.valueOf(170).doubleValue()));
assertEquals(df.format(BigDecimal.valueOf(234.56)),
df.format(BigDecimal.valueOf(234.56).doubleValue()));
assertEquals(df.format(BigDecimal.valueOf(0)),
df.format(BigDecimal.valueOf(0).doubleValue()));
assertEquals(df.format(BigDecimal.valueOf(-1)),
df.format(BigDecimal.valueOf(-1).doubleValue()));
assertEquals(df.format(BigDecimal.valueOf(-10000)),
df.format(BigDecimal.valueOf(-10000).doubleValue()));
assertEquals(df.format(BigDecimal.valueOf(-0.001)),
df.format(BigDecimal.valueOf(-0.001).doubleValue()));
assertEquals(df.format(BigDecimal.valueOf(1234567890.1234567)),
df.format(BigDecimal.valueOf(1234567890.1234567).doubleValue()));
assertEquals(df.format(BigDecimal.valueOf(1.234567E100)),
df.format(BigDecimal.valueOf(1.234567E100).doubleValue()));
}
private void assertDecFmtWithMultiplierAndFraction(String value, int multiplier, int fraction, String expectedResult) {
DecimalFormat df = (DecimalFormat)NumberFormat.getInstance();
df.setMultiplier(multiplier);
df.setMaximumFractionDigits(fraction);
BigDecimal d = new BigDecimal(value);
assertEquals(expectedResult, df.format(d));
}
private void assertDecFmtWithMultiplierAndFractionByLocale(String value, int multiplier, int fraction, Locale locale, String expectedResult) {
DecimalFormat df = (DecimalFormat)NumberFormat.getIntegerInstance(locale);
df.setMultiplier(multiplier);
df.setMaximumFractionDigits(fraction);
BigDecimal d = new BigDecimal(value);
assertEquals(expectedResult, df.format(d));
}
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
+ "]");
}
}
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
+ "]");
}
}
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
+ "]");
}
}
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
+ "]");
}
}
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
+ "]");
}
}
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
+ "]");
}
}
/**
* Returns a newly created DecimalFormat object
* @param pattern the format pattern
* @param multiplier the multiplier
* @return the formatter
*/
private static DecimalFormat createDecimalFormat(String pattern, int multiplier) {
final DecimalFormat decimalFormat = new DecimalFormat(pattern);
decimalFormat.setMultiplier(multiplier);
return decimalFormat;
}
/**
* Returns a newly created DecimalFormat object
* @param pattern the format pattern
* @param multiplier the multiplier
* @return the formatter
*/
private static DecimalFormat createDecimalFormat(String pattern, int multiplier) {
final DecimalFormat decimalFormat = new DecimalFormat(pattern);
decimalFormat.setMultiplier(multiplier);
return decimalFormat;
}