Created
June 3, 2015 16:51
-
-
Save nuboat/8508933aedb399e77a0d to your computer and use it in GitHub Desktop.
Thai Currency
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* (c) nuboat.net 2012 | |
*/ | |
package net.nuboat.currency; | |
/** | |
* | |
* @author nuboat | |
*/ | |
import java.util.StringTokenizer; | |
public class Currency { | |
private String txtNum[] = {"ศูนย์", "หนึ่ง", "สอง", "สาม", "สี่", "ห้า", "หก", "เจ็ด", "แปด", "เก้า"}; | |
private String txtWeight[] = {"", "สิบ", "ร้อย", "พัน", "หมื่น", "แสน"}; | |
private String subfix; | |
private String currency; | |
private boolean isSubfix; | |
public Currency(String currency) { | |
this(currency, null); | |
} | |
public Currency(String currency, String subfix) { | |
this.currency = currency; | |
this.isSubfix = subfix != null ? true : false; | |
this.subfix = subfix; | |
} | |
public String getBath() throws NumberFormatException { | |
String number = currency; | |
if (number == null) { | |
throw new NumberFormatException("ข้อมูลเป็นค่า null (Null Value)"); | |
} | |
number = number.replaceAll(",", ""); | |
number = number.replaceAll(" ", ""); | |
if (number.equals("")) { | |
throw new NumberFormatException("ข้อมูลเป็นค่าว่าง (Blank Value)"); | |
} | |
for (int i = 0; i < number.length(); i++) { | |
if ((number.charAt(i) < '0' || number.charAt(i) > '9') && | |
number.charAt(i) != '.') { | |
throw new NumberFormatException("ข้อมูลมีตัวอักขระ (Alphabet Value)"); | |
} | |
} | |
if (number.charAt(0) == '.') { | |
number = "0" + number; | |
} | |
if (number.charAt(number.length() - 1) == '.') { | |
number += "0"; | |
} | |
StringTokenizer st = new StringTokenizer(number, "."); | |
String beforeDot = st.nextToken(); | |
String afterDot = (st.hasMoreTokens()) ? st.nextToken() : null; // ทศนิยม | |
if (st.hasMoreTokens()) { | |
throw new NumberFormatException("ทศนิยมมากกว่า 1 ตัว"); | |
} | |
boolean beforeDotIsZero = isZero(beforeDot); // ดูว่าหน้าทศนิยมเป็น 0 หรือไม่ | |
boolean afterDotIsZero = (afterDot == null) || // ดูว่าหลังทศนิยมเป็น 0 หรือไม่ | |
(afterDot != null && isZero(afterDot)); | |
if (beforeDotIsZero && afterDotIsZero) { | |
StringBuilder zero = new StringBuilder(0); | |
zero.append("ศูนย์บาท"); | |
if (this.isSubfix) { | |
zero.append(this.subfix); | |
} | |
return zero.toString(); | |
} | |
StringBuilder result = new StringBuilder(0); | |
if (!beforeDotIsZero) { | |
String[] beforeDotArr = splitString(beforeDot); | |
for (int i = 0; i < beforeDotArr.length; i++) { | |
result.append(genStringNumber(beforeDotArr[i])); | |
if (i + 1 < beforeDotArr.length) { | |
result.append("ล้าน"); | |
} | |
else { | |
result.append("บาท"); | |
} | |
} | |
} | |
if (afterDotIsZero) { | |
if (this.isSubfix) { | |
result.append(this.subfix); | |
} | |
} else { | |
if (afterDot.length() > 2) { | |
afterDot = afterDot.substring(0, 2); | |
} | |
if (afterDot.length() == 1) { | |
afterDot += "0"; | |
} | |
String[] afterDotArr = splitString(afterDot); | |
for (int i = 0; i < afterDotArr.length; i++) { | |
result.append(genStringNumber(afterDotArr[i])); | |
if (i + 1 < afterDotArr.length) { | |
result.append("ล้าน"); | |
} | |
else { | |
result.append("สตางค์"); | |
} | |
} | |
} | |
return result.toString(); | |
} | |
private boolean isZero(String number) { | |
for (int i = 0; i < number.length(); i++) { | |
if (number.charAt(i) != '0') { | |
return false; | |
} | |
} | |
return true; | |
} | |
private String[] splitString(String number) { | |
String result[]; | |
int lengthNumber = number.length(); | |
int sizeArr = lengthNumber / 6; | |
int n = lengthNumber % 6; | |
sizeArr += n > 0 ? 1 : 0; | |
result = new String[sizeArr]; | |
int indexString = 0; | |
int indexArr = 0; | |
if (n > 0) { | |
result[indexArr++] = number.substring(indexString, n); | |
indexString += n; | |
} | |
for (int i = indexArr; i < sizeArr; i++) { | |
result[indexArr++] = number.substring(indexString, indexString + 6); | |
indexString += 6; | |
} | |
return result; | |
} | |
private String genStringNumber(String number) { | |
StringBuilder result = new StringBuilder(0); | |
int length = number.length(); | |
for (int i = 0; i < length; i++) { | |
char ch = number.charAt(i); | |
if (ch != '0') { | |
if (i == (length - 1) && ch == '1' && | |
(Integer.parseInt(number) % 100 > 10)) { | |
result.append("เอ็ด"); // ดัก เลข 1 ในกรณีที่มากกว่า 10 ให้แสดงเป็น เอ็ด | |
} else if (i == (length - 2) && ch == '2') { | |
result.append("ยี่"); // ดักเลข 20 -> ยี่สิบ | |
} else if (i == (length - 2) && ch == '1') { | |
result.append(""); // ดักเลข 10 -> สิบ ไม่ใช่ หนึ่งสิบ | |
} else { | |
result.append(this.txtNum[((int) ch) - 48]); | |
} | |
result.append(this.txtWeight[length - i - 1]); | |
} | |
} | |
return result.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment