|
|
@@ -0,0 +1,123 @@
|
|
|
+package com.uas.report.util;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 数字转为英文金额(用于UAS系统的模板制作)
|
|
|
+ *
|
|
|
+ * @author sunyj
|
|
|
+ * @since 2017年7月13日 上午9:57:00
|
|
|
+ */
|
|
|
+public class NumberParser {
|
|
|
+
|
|
|
+ private static final String[] SINGLE_NUM_ARR = { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
|
|
|
+ "Nine" };
|
|
|
+
|
|
|
+ private static final String[] TEN_NUM_ARR = { "Ten", "Eleven", "Tweleve", "Thirteen", "Fourteen", "Fifteen",
|
|
|
+ "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
|
|
|
+
|
|
|
+ private static final String[] TEN_INTEGER_ARR = { "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy",
|
|
|
+ "Eighty", "Ninety" };
|
|
|
+
|
|
|
+ public static String parse(String x) {
|
|
|
+ if (Double.parseDouble(x) <= 0.0D) {
|
|
|
+ return "Zero Cents only";
|
|
|
+ }
|
|
|
+ int z = x.indexOf(".");
|
|
|
+ String lstr = "";
|
|
|
+ String rstr = "";
|
|
|
+ if (z > -1) {
|
|
|
+ lstr = x.substring(0, z);
|
|
|
+ rstr = x.substring(z + 1);
|
|
|
+ } else {
|
|
|
+ lstr = x;
|
|
|
+ }
|
|
|
+ String lstrrev = reverse(lstr);
|
|
|
+ String[] a = new String[5];
|
|
|
+
|
|
|
+ switch (lstrrev.length() % 3) {
|
|
|
+ case 1:
|
|
|
+ lstrrev = lstrrev + "00";
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ lstrrev = lstrrev + "0";
|
|
|
+ }
|
|
|
+
|
|
|
+ String lm = "";
|
|
|
+ for (int i = 0; i < lstrrev.length() / 3; i++) {
|
|
|
+ a[i] = reverse(lstrrev.substring(3 * i, 3 * i + 3));
|
|
|
+ if (!a[i].equals("000")) {
|
|
|
+ if (i != 0) {
|
|
|
+ lm = transThree(a[i]) + " " + parseMore(String.valueOf(i)) + " " + lm;
|
|
|
+ } else
|
|
|
+ lm = transThree(a[i]);
|
|
|
+ } else {
|
|
|
+ lm = lm + transThree(a[i]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String xs = "";
|
|
|
+ if (z > -1) {
|
|
|
+ String transTwo = transTwo(rstr);
|
|
|
+ if ((transTwo == null) || ("".equals(transTwo))) {
|
|
|
+ xs = "";
|
|
|
+ } else {
|
|
|
+ xs = "and " + transTwo + " Cents ";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return lm.trim() + " " + xs + "only";
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String parseFirst(String s) {
|
|
|
+ return SINGLE_NUM_ARR[Integer.parseInt(s.substring(s.length() - 1))];
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String parseTeen(String s) {
|
|
|
+ return TEN_NUM_ARR[(Integer.parseInt(s) - 10)];
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String parseTen(String s) {
|
|
|
+ return TEN_INTEGER_ARR[(Integer.parseInt(s.substring(0, 1)) - 1)];
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String transTwo(String s) {
|
|
|
+ String value = "";
|
|
|
+
|
|
|
+ if (s.length() > 2) {
|
|
|
+ s = s.substring(0, 2);
|
|
|
+ } else if (s.length() < 2) {
|
|
|
+ s = s + "0";
|
|
|
+ }
|
|
|
+ if (s.startsWith("0")) {
|
|
|
+ value = parseFirst(s);
|
|
|
+ } else if (s.startsWith("1")) {
|
|
|
+ value = parseTeen(s);
|
|
|
+ } else if (s.endsWith("0")) {
|
|
|
+ value = parseTen(s);
|
|
|
+ } else
|
|
|
+ value = parseTen(s) + " " + parseFirst(s);
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String parseMore(String s) {
|
|
|
+ String[] a = { "", "Thousand", "Million", "Billion" };
|
|
|
+ return a[Integer.parseInt(s)];
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String transThree(String s) {
|
|
|
+ String value = "";
|
|
|
+ if (s.startsWith("0")) {
|
|
|
+ value = transTwo(s.substring(1));
|
|
|
+ } else if (s.substring(1).equals("00")) {
|
|
|
+ value = parseFirst(s.substring(0, 1)) + " Hundred";
|
|
|
+ } else
|
|
|
+ value = parseFirst(s.substring(0, 1)) + " Hundred and " + transTwo(s.substring(1));
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String reverse(String s) {
|
|
|
+ char[] aChr = s.toCharArray();
|
|
|
+ StringBuffer tmp = new StringBuffer();
|
|
|
+ for (int i = aChr.length - 1; i >= 0; i--) {
|
|
|
+ tmp.append(aChr[i]);
|
|
|
+ }
|
|
|
+ return tmp.toString();
|
|
|
+ }
|
|
|
+}
|