คือว่าจากโค๊ดมันก็แปลงได้แล้วครับ แต่ติดตรงหลักพันกับหลักแสน ครับ
package Test;
import java.text.DecimalFormat;
import java.util.Scanner;
public class EnglishNumberToWords {
private static final String[] tensNames = {
"",
"สิบ",
"ยี่สิบ",
"สามสิบ",
"สี่สิบ",
"ห้าสิบ",
"หกสิบ",
"เจ็ดสิบ",
"แปดสิบ",
"เก้าสิบ"
};
private static final String[] numNames = {
"",
"หนึ่ง",
"สอง",
"สาม",
"สี่",
"ห้า",
"หก",
"เจ็ด",
"แปด",
"เก้า",
"สิบ",
"สิบเอ็ด",
"สิบสอง",
"สิบสาม",
"สิบสี่",
"สิบห้า",
"สิบหก",
"สิบเจ็ด",
"สิบแปด",
"สิบเก้า"
};
private EnglishNumberToWords() {}
private static String convertLessThanOneThousand(int number) {
String soFar;
if (number % 100 < 20){
soFar = numNames[number % 100];
number /= 100;
}
else {
soFar = numNames[number % 10];
number /= 10;
soFar = tensNames[number % 10] + soFar;
number /= 10;
}
if (number == 0) return soFar;
return numNames[number] + "ร้อย" + soFar;
}
public static String convert(long number) {
// 0 to 999 999 999 999
if (number == 0) { return " 0"; }
String snumber = Long.toString(number);
// pad with "0"
String mask = "000000000000";
DecimalFormat df = new DecimalFormat(mask);
snumber = df.format(number);
// XXXnnnnnnnnn
int billions = Integer.parseInt(snumber.substring(0,3));
// nnnXXXnnnnnn
int millions = Integer.parseInt(snumber.substring(3,6));
// nnnnnnXXXnnn
int hundredThousands = Integer.parseInt(snumber.substring(6,9));
// nnnnnnnnnXXX
int thousands = Integer.parseInt(snumber.substring(9,12));
String tradBillions;
switch (billions) {
case 0:
tradBillions = "";
break;
case 1 :
tradBillions = convertLessThanOneThousand(billions)
+ "พันล้าน ";
break;
default :
tradBillions = convertLessThanOneThousand(billions)
+ "พันล้าน ";
}
String result = tradBillions;
String tradMillions;
switch (millions) {
case 0:
tradMillions = "";
break;
case 1 :
tradMillions = convertLessThanOneThousand(millions)
+ "ล้าน ";
break;
default :
tradMillions = convertLessThanOneThousand(millions)
+ "ล้าน ";
}
result = result + tradMillions;
String tradHundredThousands;
switch (hundredThousands) {
case 0:
tradHundredThousands = "";
break;
case 1 :
tradHundredThousands = "หนึ่งพัน ";
break;
default :
tradHundredThousands = convertLessThanOneThousand(hundredThousands)
+ "พัน ";
}
result = result + tradHundredThousands;
String tradThousand;
tradThousand = convertLessThanOneThousand(thousands);
result = result + tradThousand;
// remove extra spaces!
return result.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " ");
}
/**
* testing
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Input The Number : ");
int sc1 = sc.nextInt();
System.out.println("************************");
System.out.println("*** " + EnglishNumberToWords.convert(sc1));
System.out.println("************************");
}
}
ใครเก่ง ภาษา JAVA ครับ ช่วยแก้โค๊ดทีครับ ในการแปลงตัวเลขเป็นตัวหนังสือ
package Test;
import java.text.DecimalFormat;
import java.util.Scanner;
public class EnglishNumberToWords {
private static final String[] tensNames = {
"",
"สิบ",
"ยี่สิบ",
"สามสิบ",
"สี่สิบ",
"ห้าสิบ",
"หกสิบ",
"เจ็ดสิบ",
"แปดสิบ",
"เก้าสิบ"
};
private static final String[] numNames = {
"",
"หนึ่ง",
"สอง",
"สาม",
"สี่",
"ห้า",
"หก",
"เจ็ด",
"แปด",
"เก้า",
"สิบ",
"สิบเอ็ด",
"สิบสอง",
"สิบสาม",
"สิบสี่",
"สิบห้า",
"สิบหก",
"สิบเจ็ด",
"สิบแปด",
"สิบเก้า"
};
private EnglishNumberToWords() {}
private static String convertLessThanOneThousand(int number) {
String soFar;
if (number % 100 < 20){
soFar = numNames[number % 100];
number /= 100;
}
else {
soFar = numNames[number % 10];
number /= 10;
soFar = tensNames[number % 10] + soFar;
number /= 10;
}
if (number == 0) return soFar;
return numNames[number] + "ร้อย" + soFar;
}
public static String convert(long number) {
// 0 to 999 999 999 999
if (number == 0) { return " 0"; }
String snumber = Long.toString(number);
// pad with "0"
String mask = "000000000000";
DecimalFormat df = new DecimalFormat(mask);
snumber = df.format(number);
// XXXnnnnnnnnn
int billions = Integer.parseInt(snumber.substring(0,3));
// nnnXXXnnnnnn
int millions = Integer.parseInt(snumber.substring(3,6));
// nnnnnnXXXnnn
int hundredThousands = Integer.parseInt(snumber.substring(6,9));
// nnnnnnnnnXXX
int thousands = Integer.parseInt(snumber.substring(9,12));
String tradBillions;
switch (billions) {
case 0:
tradBillions = "";
break;
case 1 :
tradBillions = convertLessThanOneThousand(billions)
+ "พันล้าน ";
break;
default :
tradBillions = convertLessThanOneThousand(billions)
+ "พันล้าน ";
}
String result = tradBillions;
String tradMillions;
switch (millions) {
case 0:
tradMillions = "";
break;
case 1 :
tradMillions = convertLessThanOneThousand(millions)
+ "ล้าน ";
break;
default :
tradMillions = convertLessThanOneThousand(millions)
+ "ล้าน ";
}
result = result + tradMillions;
String tradHundredThousands;
switch (hundredThousands) {
case 0:
tradHundredThousands = "";
break;
case 1 :
tradHundredThousands = "หนึ่งพัน ";
break;
default :
tradHundredThousands = convertLessThanOneThousand(hundredThousands)
+ "พัน ";
}
result = result + tradHundredThousands;
String tradThousand;
tradThousand = convertLessThanOneThousand(thousands);
result = result + tradThousand;
// remove extra spaces!
return result.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " ");
}
/**
* testing
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Input The Number : ");
int sc1 = sc.nextInt();
System.out.println("************************");
System.out.println("*** " + EnglishNumberToWords.convert(sc1));
System.out.println("************************");
}
}