2011년 10월 27일 목요일

JAVA_소수점 둘째짜리에서 자르기

DecimalFormat df = new DecimalFormat("######0.00");
df.format(소수점 타입의 데이타);

2011년 10월 18일 화요일

JAVA_코딩 테크닉_자바 백분율 예제

for (Iterator<Object> i = tArr.iterator();i.hasNext();)
{
hm = (HashMap<String, Object>)i.next();
tempResultTotal += Double.parseDouble(String.valueOf(hm.get("RESULT_TOTAL")));
}

=========================================================================


HashMap map = (HashMap)itr.next();


resultPer = Double.parseDouble(map.get("RESULT_PER").toString());
planPer = Double.parseDouble(map.get("PLAN_PER").toString());

if(resultPer < 1)
{
resultPer = 0*0.01d;
}

if(planPer > 0)
{
resultPerTotal = resultPer / planPer;
bDecimal = new BigDecimal(resultPerTotal).setScale(2, BigDecimal.ROUND_HALF_UP);
resultPerTotal = Double.parseDouble(bDecimal.toString());
}
else
{
planPer = 0*0.01d;
}


tempResult = Double.parseDouble(String.valueOf(map.get("RESULT_TOTAL")));

if(tempResultTotal > 0)
{
tempResult = tempResult / tempResultTotal * 100;
bDecimal = new BigDecimal(tempResult).setScale(2, BigDecimal.ROUND_HALF_UP);
tempResult = Double.parseDouble(bDecimal.toString());


}
else
{
tempResult = 0*0.01d;
}


tempPlan = Double.parseDouble(String.valueOf(map.get("PLAN_PER_TOTAL")));
if(tempPlan > 0)
{
tempPeref = tempResult / tempPlan ;
bDecimal = new BigDecimal(tempPeref).setScale(2, BigDecimal.ROUND_HALF_UP);
tempPeref = Double.parseDouble(bDecimal.toString());
}

2011년 10월 14일 금요일

JAVA_코딩 테크닉 자바에서 소수점 자리 계산

자바에서 소수점 자리 계산P.L(file)
2006/10/19 10:50

소수점 둘째자리 까지 값을 구하기..
1. BigDecimal2. DecimalFormat


import java.math.BigDecimal;
import java.text.DecimalFormat;

double rate = (double)37/6;

System.out.println("---->rate : " + rate);
BigDecimal bD = new BigDecimal(rate);

System.out.println("------>BigDecimal : " + bD);
bD.setScale(2, java.math.BigDecimal.ROUND_HALF_UP); //소수점 2자리 ,반올림..

System.out.println("------>setScale : " + bD.setScale(2, java.math.BigDecimal.ROUND_HALF_UP));
DecimalFormat fourDigits = new DecimalFormat("0.00"); //소수점 밑 2자리까지
String A = fourDigits.format(rate);

System.out.println("DecimalFormat: "+ A);

===============================================================================
결과
===============================================================================
------->rate : 6.166666666666667

---------->BigDecimal : 6.16666666666666696272613990004174411296844482421875

---------->setScale : 6.17

DecimalFormat: 6.17

2011년 10월 10일 월요일

JAVA_코딩 테크닉 20110811.substring (begin, end) 년,월,일 자르기

할때마다 해깔린다..


cal.set(Integer.parseInt(startDate.substring(0, 4)),
Integer.parseInt(startDate.substring(4, 6)),
Integer.parseInt(startDate.substring(6, 8)));