ตัวอย่างโคด
package demo; /** * @author nopphanan7 * */ public final class ThaiNumber { private static final String[] DIGIT_TH = { "0", "๑", "๒", "๓", "๔", "๕", "๖", "๗", "๘", "๙" }; private String valueText; // ···········Methods·············· public String getText(double amount) { this.valueText = getThaiNumber(String.valueOf(amount)); return this.valueText; } public String getText(float amount) { this.valueText = getThaiNumber(String.valueOf(amount)); return this.valueText; } public String getText(int amount) { this.valueText = getThaiNumber(String.valueOf(amount)); return this.valueText; } public String getText(long amount) { this.valueText = getThaiNumber(String.valueOf(amount)); return this.valueText; } public String getText(String amount) { this.valueText = getThaiNumber(amount.trim()); return this.valueText; } public String getText(Number amount) { this.valueText = getThaiNumber(String.valueOf(amount)); return this.valueText; } private static String getThaiNumber(String amount) { if(amount == null || amount.isEmpty()) return ""; StringBuilder sb = new StringBuilder(); for(char c : amount.toCharArray()){ if(Character.isDigit(c)){ String index = DIGIT_TH[Character.getNumericValue(c)].toString(); sb.append(index); } else { sb.append(c); } } return sb.toString(); } /** * @param args */ public static void main(String[] args) { System.out.println("Negative value of -1257.5463 : " + new ThaiNumber().getText(-1257.5463)); System.out.println("Positive value of 1234.5463 : " + new ThaiNumber().getText(1234.5463)); System.out.println("Negative string value of -1,234.5463 : " + new ThaiNumber().getText("-1,234.5463")); System.out.println("Positive string value of 1,234.5463 : " + new ThaiNumber().getText("1,234.5463")); } }ผลลัพธ์ที่ได้คือ
Negative value of -1257.5463 : -๑๒๕๗.๕๔๖๓ Positive value of 1234.5463 : ๑๒๓๔.๕๔๖๓ Negative string value of -1,234.5463 : -๑,๒๓๔.๕๔๖๓ Positive string value of 1,234.5463 : ๑,๒๓๔.๕๔๖๓
0 comments:
Post a Comment