on
[Java] - 자바 String 문자열 숫자에 소수점, 콤마(,) 찍기...
[Java] - 자바 String 문자열 숫자에 소수점, 콤마(,) 찍기...
반응형
#숫자에 콤마(,)를 붙여서 반환하는 메서드
#param: str String(int), format String
#return: String
public class test { // param string public static String getFormat(String str, String format) { if (format == null || format.equals("")) { format = "###,###,###,###"; } String temp = null; if (str == null || str.equals(" ")) { temp = "0"; } else { double change = Double.valueOf(str).doubleValue(); DecimalFormat decimal = new DecimalFormat(format); temp = decimal.format(change); } return temp; } // param int public static String getFormat(int istr, String format) { String str = Integer.toString(istr); if (format == null || format.equals("")) { format = "###,###,###,###"; } String temp = null; if (str == null) { temp = "0"; } else { double change = Double.valueOf(str).doubleValue(); DecimalFormat decimal = new DecimalFormat(format); temp = decimal.format(change); } return temp; } public static void main(String[] args) { System.out.println(getFormat("12345", "###,###")); // 12,345 System.out.println(getFormat(12345, "###,###")); // 12,345 System.out.println(getFormat(1200000, "###,###")); // 1,200,000 System.out.println(getFormat(1200, "#,##")); // 12,00 } }
728x90
반응형
from http://pingfanzhilu.tistory.com/318 by ccl(A) rewrite - 2021-09-28 17:27:18