Java字符串转化为货币为中文大写

回复
  • 尹静晨用户
    [[{"txt": "Java\u5b57\u7b26\u4e32\u8f6c\u5316\u4e3a\u8d27\u5e01\u4e3a\u4e2d\u6587\u5927\u5199"}], [{"txt": ["\u5de5\u5177/\u539f\u6599"]}, {"txt": "myeclipse8.5"}, {"txt": "Java"}, {"txt": "JDK1.6"}], [{"txt": ["\u65b9\u6cd5/\u6b65\u9aa4"]}, {"txt": "package l_homewok;import java.text.DecimalFormat;import java.util.Scanner;/**\u00a0* \u7f16\u7a0b\u5b9e\u73b0\u91d1\u989d\u8f6c\u6362\uff0c\u952e\u76d8\u8f93\u5165\u963f\u62c9\u4f2f\u6570\u5b57\u7684\u91d1\u989d\u8f6c\u6362\u6210\u4e2d\u56fd\u8d22\u52a1\u5927\u5199\u7684\u5f62\u5f0f\u5982\uff1a \uff08\uffe57077\uff09\uff0d>\uff08\u67d2\u4edf\u96f6\u67d2\u62fe\u67d2\u5143\u6574\uff09\u8f93\u51fa \u5173\u952e\u6280\u672f\uff1a\u00a0* 1.\u5c06\u6570\u5b57\u683c\u5f0f\u5316\uff0c\u5982\u679c\u5b58\u5728\u5c0f\u6570\u90e8\u5206\uff0c\u5c06\u5176\u88c5\u6362\u4e3a3\u4f4d\u5c0f\u6570\u5230\u5355\u4f4d\u5398\u3002 2.\u5206\u522b\u5c06\u6574\u6570\u90e8\u5206\u4e0e\u5c0f\u6570\u90e8\u5206\u88c5\u6362\u4e3a\u4e3a\u5927\u5199\u90e8\u5206\uff0c\u5e76\u63d2\u5165\u5176\u5355\u4f4d\uff08\u4ebf\u3001\u4e07\u3001\u4edf......\uff09\u3002\u00a0* 3.\u7ec4\u5408\u88c5\u6362\u540e\u7684\u6574\u6570\u90e8\u5206\u548c\u5c0f\u6570\u90e8\u5206\u3002\u00a0*\u00a0\u00a0* @author lenovo\u00a0*\u00a0\u00a0*/public class L_Homework4{\tprivate final static String[] STR_NUMBER = { \"\u96f6\", \"\u58f9\", \"\u8d30\", \"\u53c1\", \"\u8086\", \"\u4f0d\",\t\"\u9646\", \"\u67d2\", \"\u634c\", \"\u7396\" };\tprivate final static String[] STR_UNIT = { \"\", \"\u62fe\", \"\u4f70\", \"\u4edf\", \"\u4e07\", \"\u62fe\",\t\"\u4f70\", \"\u4edf\", \"\u4ebf\", \"\u62fe\", \"\u4f70\", \"\u4edf\" };// \u6574\u6570\u5355\u4f4d\tprivate final static String[] STR_UNIT2 = { \"\u89d2\", \"\u5206\", \"\u5398\" };// \u5c0f\u6570\u5355\u4f4d\tpublic static void main(String[] args)\t{\t\twhile (true)\t\t{\t\t\tString convert = convert(getScanner());\t\t\tSystem.out.println(convert);\t\t}\t}\t// 1.\u91d1\u989d\u8f6c\u6362\u65b9\u6cd5convert();\tpublic static String convert(double m)\t{\t\t// \u5b9e\u4f8b\u5316DecimalFormat\u5bf9\u8c61\t\tDecimalFormat df = new DecimalFormat(\"#0.###\");\t\t// \u683c\u5f0f\u5316double\u6570\u5b57\t\tString strNum = df.format(m);\t\t// \u5224\u65ad\u662f\u5426\u5305\u542b\u5c0f\u6570\u70b9\t\tif (strNum.indexOf(\".\") != -1)\t\t{\t\t\tString num = strNum.substring(0, strNum.indexOf(\".\"));// \u53d6\u6574\u6570\u90e8\u5206\t\t\t// \u6574\u6570\u90e8\u5206\u5927\u4e8e12\u4f4d\u4e0d\u80fd\u88c5\u6362\uff08\u5373\u5355\u4f4d\u7ea7\u522b\u5927\u4e8e\u5343\u4ebf\uff09\t\t\tif (num.length() > 12)\t\t\t{\t\t\t\tSystem.out.println(\"\u91d1\u989d\u592a\u5927\uff0c\u4e0d\u80fd\u5b8c\u6210\u88c5\u6362\uff01\");\t\t\t\treturn \"\";\t\t\t}\t\t}\t\tString point = null; // \u5c0f\u6570\u70b9\t\tif (strNum.indexOf(\".\") != -1)\t\t{\t\t\tpoint = \"\u5143\";\t\t} else\t\t{\t\t\tpoint = \"\u5143\u6574\";\t\t}\t\t// \u8f6c\u6362\u7ed3\u679c\t\tString result = getIntger(strNum) + point + getDecimal(strNum);\t\tif (result.startsWith(\"\u5143\"))\t\t{// \u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u4ee5\u5143\u4e3a\u5f00\u59cb-\u5373\u5c0f\u4e8e\u4e00\u5143\t\t\tresult = result.substring(1, result.length());// \u622a\u53d6\u5b57\u7b26\u4e32\t\t}\t\treturn result;\t}\t// 2.\u8f6c\u6362\u6574\u6570\u90e8\u5206\tprivate static String getIntger(String num)\t{\t\tif (num.indexOf(\".\") != -1)\t\t{\t\t\tnum = num.substring(0, num.indexOf(\".\"));\t\t}\t\tnum = new StringBuilder(num).reverse().toString();// \u7ffb\u8f6c\u5b57\u7b26\u4e32\t\tStringBuilder temp = new StringBuilder();\t\tfor (int i = 0; i < num.length(); i++)\t\t{\t\t\ttemp.append(STR_UNIT[i]);\t\t\ttemp.append(STR_NUMBER[num.charAt(i) - 48]);// num.charAt(i)-48\u83b7\u53d6\u6570\u503c\t\t\t// \u6216\u8005\u4f7f\u7528Integer.pa\u2026\u2026\t\t}\t\tnum = temp.reverse().toString();// \u7ffb\u8f6c\u5b57\u7b26\u4e32\t\twhile (true)\t\t{\t\t\t// \u66ff\u6362\u5b57\u7b26\u4e32\t\t\tnum = num.replaceAll(\"\u96f6\u62fe\", \"\u96f6\");// \u66ff\u6362\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\t\t\tnum = num.replaceAll(\"\u96f6\u4f70\", \"\u96f6\");\t\t\tnum = num.replaceAll(\"\u96f6\u4edf\", \"\u96f6\");\t\t\tnum = num.replaceAll(\"\u96f6\u4e07\", \"\u4e07\");\t\t\tnum = num.replaceAll(\"\u96f6\u4ebf\", \"\u4ebf\");\t\t\tnum = num.replaceAll(\"\u96f6\u96f6\", \"\u96f6\");\t\t\tnum = num.replaceAll(\"\u4ebf\u4e07\", \"\u4ebf\");\t\t\t// \u5224\u65ad\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5305\u542b\u6307\u5b9a\u5b57\u7b26\t\t\tif (num.indexOf(\"\u96f6\u96f6\") == -1)\t\t\t{\t\t\t\tbreak;\t\t\t}\t\t}\t\tif (num.lastIndexOf(\"\u96f6\") == num.length() - 1)\t\t{// \u5982\u679c\u5b57\u7b26\u4e32\u4ee5\u96f6\u7ed3\u5c3e\u5c06\u5176\u51fa\u53bb\t\t\tnum = num.substring(0, num.length() - 1);\t\t}\t\treturn num;\t}\t// 3.\u8f6c\u6362\u5c0f\u6570\u90e8\u5206\tprivate static String getDecimal(String num)\t{\t\t// \u5224\u65ad\u662f\u5426\u5305\u542b\u5c0f\u6570\u70b9\t\tif (num.indexOf(\".\") == -1)\t\t{\t\t\treturn \"\";\t\t}\t\tnum = num.substring(num.indexOf(\".\") + 1);\t\t// \u53cd\u8f6c\u5b57\u7b26\u4e32\t\t// num = new StringBuffer(num).reverse().toString();\t\tnum = new StringBuffer(num).toString();\t\tStringBuffer temp = new StringBuffer();\t\t// \u52a0\u5165\u5355\u4f4d\t\tfor (int i = 0; i < num.length(); i++)\t\t{\t\t\ttemp.append(STR_NUMBER[num.charAt(i) - 48]);\t\t\ttemp.append(STR_UNIT2[i]);\t\t}\t\tnum = temp.toString();\t\twhile (true)\t\t{\t\t\t// \u66ff\u6362\u5b57\u7b26\u4e32\t\t\tnum = num.replaceAll(\"\u96f6\u89d2\", \"\u96f6\"); // \u66ff\u6362\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\t\t\tnum = num.replaceAll(\"\u96f6\u5206\", \"\u96f6\"); // \u66ff\u6362\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\t\t\tnum = num.replaceAll(\"\u96f6\u5398\", \"\u96f6\"); // \u66ff\u6362\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\t\t\tnum = num.replaceAll(\"\u96f6\u96f6\", \"\u96f6\"); // \u66ff\u6362\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\t\t\t// \u5224\u65ad\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5305\u542b\u6307\u5b9a\u5b57\u7b26\t\t\tif (num.indexOf(\"\u96f6\u96f6\") == -1)\t\t\t{\t\t\t\tbreak;\t\t\t}\t\t}\t\t// \u5982\u679c\u5b57\u7b26\u4e32\u4ee5\u96f6\u7ed3\u5c3e\u5c06\u5176\u9664\u53bb\t\tif (num.lastIndexOf(\"\u96f6\") == num.length() - 1)\t\t{\t\t\tnum = num.substring(0, num.length() - 1);\t\t}\t\treturn num;\t}\tpublic static double getScanner()\t{\t\tScanner in = new Scanner(System.in);\t\tSystem.out.println(\"\u8bf7\u8f93\u5165\u4e00\u4e2a\u91d1\u989d\uff1a\");\t\treturn in.nextDouble();\t}}"}], [{"txt": ["\u6ce8\u610f\u4e8b\u9879"]}, {"txt": "\u6b64\u4ee3\u7801\u53ef\u4f18\u5316\uff0c\u8bf7\u5b66\u4e60\u8005\u81ea\u884c\u641e\u5b9a"}]]
    2024-05-12
    1楼
    回复

参与评论

游客评论不支持回复他人评论内容,如需回复他人评论内容请