Thursday, June 4, 2009

java decimal format

java.text.DecimalFormat dfm = new java.text.DecimalFormat("0.00");

double d = 0.123d;
System.out.println(dfm.format(d)); // 0.12

d = 0.129d;
System.out.println(dfm.format(d)); // 0.13

d = new Double(dfm.format(d)).doubleValue();
System.out.println(d); // 0.13


OR ...

DecimalFormat changeFormat = new DecimalFormat("#,##0.00");

double a = 20213243;

BigDecimal aa = new BigDecimal(a);

BigDecimal divideA = aa.divide(new BigDecimal(3),4,4);

System.out.println("divideA = "+divideA);
System.out.println("Result = "+changeFormat.format(divideA));


result:
divideA = 6737747.6667
Result = 6,737,747.67

ref: http://www.narisa.com/forums/index.php?showtopic=12589&st=0

No comments:

Post a Comment