2007年4月17日星期二

java format

java实现四舍五入:
int iTax ;
double
dCash = 0.88;
//BigDecimal的构造函数参数类型是double
BigDecimal deTax = new BigDecimal(dCash);
//deTax.setScale(0,BigDecimal.ROUND_HALF_UP ) 返回值类型 BigDecimal
//intValue() 方法将BigDecimal转化为int
iTax = deTax.setScale(0,BigDecimal.ROUND_HALF_UP).intValue();
java日期格式化:
/*
Description 格式化显示日期型数据
@param Date dt_in 日期型数据
boolean bShowTimePart_in 是否显
示时间部分
@return String
*/

public String DoFormatDate(java.util.Date dt_in, boolean bShowTimePart_in) {
if (bShowTimePart_in)
return (new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")).format(dt_in);
else
return (new SimpleDateFormat("yyyy-MM-dd")).format(dt_in);
}
java保留两位小数:
DecimalFormat fnum = new DecimalFormat("#0.00");
String str = fnum.format(0.123);
java格式化输出:
DecimalFormat df = new DecimalFormat("格式");
String fmt =df.format(double);
符号 意义
0 一个数位
# 一个数位,前导零和追尾零不显示
. 小数点分割位置
, 组分隔符的位置
- 负数前缀
% 用100乘,并显示百分号
其他任何符号 在输出字符串中包括指定符号
ep1
DecimalFormat df = new DecimalFormat(",##0.00");
System.out.println(df.format(1234.5));
result:1,234.50
ep2
DecimalFormat df = new DecimalFormat("$,##0.00;($,##0.00)");
System.out.println(df.format(-1234.5));//把负数装入括号中而不是用一个负号表示
result:($1,234.50)
ep3
DecimalFormat df = new DecimalFormat("0.######");
System.out.println(df.format(1234.5));
result:1234.5

没有评论: