工具类——数据保留

数据保留工具,提供是传入double类型的数据,和保留小数位点数后面的长度,返回的是字符类型

package cn.yunzhf.accounting.common.utils;

import java.text.DecimalFormat;

/**
 * 数据保留工具
 * @author xu
 *
 */
public class DataRetain {
	/**
	 * 转换成为字符串
	 * @param a 数值
	 * @param length 长度
	 * @return String 字符
	 */
	public static String getData(double a , int length){		
		StringBuffer sb = new StringBuffer("#,##0.");
		for (int i = 1; i <= length; i++) {
			sb.append("0");			
		}
		String string = new String(sb);
		return new DecimalFormat(string).format(new Double(a));
	}
}

猜你喜欢

转载自blog.csdn.net/ghostxbh/article/details/81133376