就是简单实用了,才工作中应用到了,怕忘记就发出来。
主要功能很简单,但是处理起来就是很繁琐,说明以下,当拿到一个拿到一个小数点(double)类型,然后进行转换百分比。
package com.ubs.ifop.service.statistics.webapp.action;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import com.ubs.ifop.service.statistics.webapp.items.PieChartItem;
public class test {
static String FORMAT_PERCENT_DEFAULT = "###,##0.00%";
private static DecimalFormat decFormatter = new DecimalFormat();
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList pieChartSolist = new ArrayList();
double amount = 0.0D;
String result = toPercentage("0.122222");
System.out.println(result);
// test
/*
* for (int i = 1; i < list.size(); i++) {
*
* String key =((String[]) list.get(i))[0]; String value = ((String[])
* list.get(i))[1]; PieChartItem so = new PieChartItem();
* so.setName(key); so.setValue(value); pieChartSolist.add(so); amount =
* amount + Double.parseDouble(value); }
*/
// 16_AMEX-LUX,82_,520_EFUND-LUX,1721_EPEX-LUX,920_EPF-LUX,11_TRADER-LUX,3059_estmt
PieChartItem so1 = new PieChartItem();
so1.setName("AMEX-LUX");
so1.setValue("16");
PieChartItem so2 = new PieChartItem();
so2.setName("CHDRPT-LUX");
so2.setValue("82");
/* PieChartItem so3 = new PieChartItem();
so3.setName("EPEX-LUX");
so3.setValue("1721");
PieChartItem so4 = new PieChartItem();
so4.setName("EPF-LUX");
so4.setValue("920");
PieChartItem so5 = new PieChartItem();
so5.setName("TRADER-LUX");
so5.setValue("11");
PieChartItem so6 = new PieChartItem();
so6.setName("estmt");
so6.setValue("3059");*/
pieChartSolist.add(so1);
pieChartSolist.add(so2);
/* pieChartSolist.add(so3);
pieChartSolist.add(so4);
pieChartSolist.add(so5);
pieChartSolist.add(so6);*/
for (int i = 0; i < pieChartSolist.size(); i++) {
PieChartItem so = (PieChartItem) pieChartSolist.get(i);
amount = amount + Double.parseDouble(so.getValue());
}
System.out.println(amount);
for (int i = 0; i < pieChartSolist.size(); i++) {
PieChartItem item = (PieChartItem) pieChartSolist.get(i);
double aa = Double.parseDouble(item.getValue());
double bb = aa / amount;
//System.out.println("bb :" + bb);
item.setTotalAmount(amount);
System.out.println(item.getValue() + " "+test.toPercentage(String.valueOf(bb)));
}
}
public static String toPercentage(String value1) {
// disabled multiplier
return formatNumeric(value1, FORMAT_PERCENT_DEFAULT, 100);
}
protected static int getDecimalPlace(String format) {
int decimalPlace = 0;
int dseparator = format.lastIndexOf(".");
if (dseparator == -1) {
// if no decimal place found, set as zero
dseparator = format.length();
} else {
// add 1 to move the character after decimal separator
dseparator += 1;
}
decimalPlace = format.length() - dseparator;
// check the % character
int percentIndex = format.indexOf("%");
if (percentIndex != -1) {
// if the format contained "%", the value will be multiplied by 100
// before display it
// then if % character found, add two digits for rounding
if (dseparator <= percentIndex) {
// if "%" found after the decimal separator, just add one
// character
decimalPlace += 1;
} else {
decimalPlace += 2;
}
}
return decimalPlace;
}
public static String formatNumeric(String value1, String format,
int multiplier) {
BigDecimal value = new BigDecimal(value1.toString());
int decimalPlace = getDecimalPlace(format);
decFormatter.applyPattern(format);
if (multiplier != 0) {
decFormatter.setMultiplier(multiplier);
}
return format(value, decimalPlace, decFormatter);
}
protected static String format(BigDecimal value, int decimalPlace,
DecimalFormat decFormat) {
BigDecimal diff = new BigDecimal("0");
BigDecimal tempValue = new BigDecimal(String.valueOf(value));
tempValue = tempValue.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
return decFormat.format(tempValue.add(diff));
}
}