프로그래밍 언어/Java
Java : 맵 데이터의 Key값을 각 형식으로 변경하는 방법
알버트
2021. 9. 13. 10:40
1. String 형태로 변경
protected String getMapToString(Map<String,Object> map, String strKey) {
if(map == null || map.isEmpty()) {
return "";
}
if(map.containsKey(strKey)) {
return map.get(strKey).toString();
}
return "";
}
2. Double(실수) 형태로 변경
protected double getMapToDouble(Map<String,Object> map, String strKey) {
String strData = getMapToString(map, strKey);
if("".equals(strData)) {
return 0;
}
return Double.parseDouble(strData);
}
3. Integer(정수) 형태로 변경
protected int getInteger(String val, int defValue) {
int iResult = 0;
try {
iResult = Integer.parseInt(val);
} catch(NumberFormatException e) {
iResult = defValue;
}
retrun iResult;
}
4. 금액 표시형식 및 소수점 2자리 실수 형태로 변경
protected String getDoubleFormat(double target) {
try {
if (target == 0) return "0";
DecimalFormat df = new DecimalFormat("#,###.##");
return df.format(Math.round(target*100)/100.0);
} catch(Exception e) {
return "0";
}
}