Package kellerdu.util;
Import java.util.hashtable; import java.util.date; import java.text.SIMPLEDATEFORMAT; import java.util.locale; import java.text.numberformat; import java.util.currency;
/ ** * Some tool functions involved in digital Chinese representations * * @version 1.0 * / public class number {private static hashtable hy = null; private static final string [] san = {"", "pick", "百" "Qian"}; private static final string [] wan = {"", "10,000", "100 million"}; private static final simpledateformat sdf = new SimpleDateFormat ("YYYY Month Month D Japanese hh: hh ee", Locale .China); Private static final Numberformat nf = Numberformat.getcurrencyinstance (locale.china); private static final string sambol = currency.getInstance (locale.china). Getsymbol ();
private number () {}
/ ** * iScurrency is true, all of the numbers all write * @Param iscurrency boolean * @Return Numcn * / public static number getInstance (boolean iscurrency) {hy = new hashtable (); if (iScurrency) {hy.put (" 0 "," zero "); hy.put (" 1 "," "); hy.put (" 2 "," 貮 "); hy.put (" 3 "," 叁 "); hy.put ("4", ""); hy.put ("5", "Wu"); hy.put ("6", "Lu"); hy.put ("7", "柒"); HY .put ("8", "捌"); hy.put ("9", "");} else {hy.put ("0", "zero"); hy.put ("1", " One "); hy.put (" 2 "," two "); hy.put (" 3 "," three "); hy.put (" 4 "," four "); hy.put (" 5 " "5"); hy.put ("6", "six"); hy.put ("7", "seven"); HY.PUT ("8", "eight"); hy.put (" 9 "," nine ");} return new numcn ();
Public static numcn getinstance () {return numcn.getinstance (false);}
/ ** * currency numbers indicate * @Param f Double * @return string * @throws exception * /
Public String Tocurrency (float f) {string s = nf.format (f); returnof
/ ** * Money uppercase digital representation * @Param f Double * @Return string * @throws exception * / public string tocncurrency (double f) throws exception {if (f <0) {throw new Exception ("can not length kilow 0 ! ");} Int = (int) f; string s = sambol translate ((long) i) " yuan "; double ff = f - i; if (ff> 0) {ff * = 10; long Fi = math.Round (ff); if (ff> = 1) {IF (Math.Abs (FF - FI) <1e-3) {s = (hy.get (string.valueof (math.Round) ))) "Angle");} else {s = (hy.get (string.valueof (math.Round (ff) - 1)) "angle");}}} (math.abs (ff - Fi)> 1e-3) {ff = (ff - (int) ff) * 10; FI = Math.Round (ff); if (ff> = 1) {IF (Math.Abs (FF - FI) <1E -3) {s = (hy.get (string.valueof (math.Round (ff))) "points");} else { S = (hy.get (math.Round (ff) - 1)) "points");}}}} Return S "whole";} / ** * translation, accuracy six * @ Param f Double * @return string * / public string translate (double f) {string result = ""; if (f <0) {result = "negative"; f = math.abs (f);} Return Result Translate (INT) F) Con (f - (int) f));}
/ ** * Rendering fractional part, accuracy six * @Param f Double * @return string * / prince String con (double f) {string result = "point"; f = f * 10; while (f <1) { Result = "zero"; f = f * 10;} int i = (int) Math.Round (f); if (i <10) {result = hy.get (String.Valueof (i));} Else {result = hy.get (String.Valueof (9));} while (math.abs (f - i)> 1e-6) {f = (f - (int) f) * 10; i = int) Math.Round (f); if (i <10) {result = hy.get (String.Valueof (i));} else {result = hy.get (String.Valueof (9));} } Return Result;
/ ** * Relationship * @Param l long * @Return string * / public string translate (long L) {string result = ""; Boolean ISFU = L <0; l = math.abs (l); string s = string .Valueof (L); int J = 0; for (int i = s.Length (); i> 0; i - = 4) {INT be = I - 4; be = be <0? 0: be; string m = ""; if ((j% 4) == 0) {IF (j! = 0) {//system.out.println (S " :" be); if ((j% 8) == 0) {m = "100 million";} else {m = "10,000";}}} // cannot have 8 billion case IF ((M.EQUALS ("100 million" && result.charat (0) = = '10) {result = result.replacefirst ("10,000", "100 million"); m = "";} result = qian (s.Substring (be, i)) m result; J = 4 } If (isfu) {result = "negative" result;} return RESULT;
/ ** * Translate 9999-0000 * @Param s string * @return string * / private string qian (string s) {string r = ""; char [] cs = new char [1]; boolean Find = false; for (INT i = s.Length () - 1; i> -1; i--) {// from the right number, find a bit of IF (! find) {if (S.Charat (i) == '0') {Continue;} else {find = true;}} cs [0] = S.Charat (i); if (cs [0] == '0') {IF (S.Charat (i 1 ) == 0 '|| i == 0) {Continue;} else {r = (string) hy.get (new string (cs)) r;}} else {r = (string) hy.get NEW STRING (CS)) SAN [S.Length () - 1 - i] r;}} f (") {r = (string) hy.get (" 0 ");} return r;} public string formate (date date) {return SDF.form AT (date);
}