实现浮点数转换成人民币读法字符串(Java)

末蓝、 2021-06-24 13:59 452阅读 0赞

public class NumberToRMB {

  1. private final String\[\] hanArr = \{"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"\};
  2. private final String\[\] unitArr = \{"仟", "", "十", "佰"\};
  3. private final String\[\] tag = \{"元", "万", "亿"\};
  4. public String toHanStr(double num) \{
  5. String result = "";
  6. long zheng = (long)num;
  7. //long xiao = Math.round((num - zheng) \* 100);//如果需要四舍五入,就用这个方法,但是小数点后
  8. //两位依次为99时,第三位不能>=5,否则会报错
  9. long xiao = (long)((num - zheng) \* 100);//如果不需要四舍五入,就用这个方法
  10. /\*整数部分为0单独考虑\*/
  11. if(0 == zheng) \{
  12. int tempJiao = (int)(xiao / 10);
  13. int tempFen = (int)(xiao % 10);
  14. if(tempJiao != 0) \{
  15. result += hanArr\[tempJiao\] + "角";
  16. \}
  17. if(tempFen != 0) \{
  18. result += hanArr\[tempFen\] + "分";
  19. \}
  20. return result;
  21. \}
  22. String zhengStr = String.valueOf(zheng);
  23. int len = zhengStr.length();
  24. int tempLen = 0;
  25. /\*处理整数部分\*/
  26. for(int i = 0; i < len; i++) \{
  27. int temp = zhengStr.charAt(i) - 48;
  28. int part = (len - i - 1) / 4; //当前字符处于哪个段
  29. int location = (len - i - 1) % 4; //当前字符处于该段的具体哪个位置
  30. if(location != 0) \{ //不是该段的最后一个
  31. if(temp != 0) \{ //当前字符不为0
  32. result += hanArr\[temp\] + unitArr\[(len - i) % 4\]; continue;
  33. \} else \{ //当前字符为0
  34. tempLen = result.length();
  35. if(3 == location && result.charAt(tempLen - 1) != '零') \{ //当前字符为0且为该段第一个
  36. result += "零"; continue;
  37. \} else \{
  38. tempLen = result.length();
  39. if(result.charAt(tempLen - 1) == '零') \{
  40. continue;
  41. \} else \{
  42. result += "零"; continue;
  43. \}
  44. \}
  45. \}
  46. \} else \{ //是该段最后一个
  47. if(temp != 0) \{
  48. result += hanArr\[temp\] + tag\[part\]; continue;
  49. \} else \{
  50. tempLen = result.length();
  51. if(result.charAt(tempLen - 1) != '零') \{
  52. result += tag\[part\]; continue;
  53. \} else \{
  54. if(result.charAt(tempLen - 2) == '亿' || result.charAt(tempLen - 2) == '万') \{
  55. continue;
  56. \} else \{
  57. result = result.substring(0, tempLen - 1) + tag\[part\]; continue;
  58. \}
  59. \}
  60. \}
  61. \}
  62. \}
  63. /\*处理小数部分\*/
  64. int jiao = (int)(xiao / 10);
  65. int fen = (int)(xiao % 10);
  66. if(jiao != 0) \{
  67. result += hanArr\[jiao\] + "角";
  68. \}
  69. if(fen != 0) \{
  70. result += hanArr\[fen\] + "分";
  71. \}
  72. /\*排除输入形式如60 0000 0000.00,输出为“陆十亿零”的情况\*/
  73. tempLen = result.length();
  74. if(result.charAt(tempLen - 1) == '零') \{
  75. result = result.substring(0, tempLen - 1);
  76. \}
  77. tempLen = result.length();
  78. if(result.charAt(tempLen - 1) == '元') \{
  79. result += "整";
  80. \}
  81. if(result.charAt(tempLen - 1) == '万' || result.charAt(tempLen - 1) == '亿') \{
  82. result += "元整";
  83. \}
  84. return result;
  85. \}
  86. public static void main(String\[\] args) \{
  87. NumberToRMB ntr = new NumberToRMB();
  88. System.out.println(ntr.toHanStr(6000000000.0000));
  89. System.out.println(ntr.toHanStr(600100.000));
  90. System.out.println(ntr.toHanStr(1006.333));
  91. System.out.println(ntr.toHanStr(10.111));
  92. \}

}

部分算法如下:

  1. int len = zhengStr.length();//取整数部分字符串长度
  2. for(i = 0; i < len; i++)//遍历整个整数字符串
  3. int temp = zhengStr.charAt(i) - 48;//当前字符转为数值
  4. int part = (len - i - 1) / 4;//当前字符位于那一段
  5. int location = (len - i - 1) % 4;//当前字符处于段的那一个位置
  6. if 当前字符不是本段最后一个
  7. if 当前字符不为‘0 result += 转换成的繁体字 + 单位; continue;
  8. else 当前字符为‘0
  9. if 当前字符是该段第一个字符并且result串的最后一个字符!='零' result += '零';continue;
  10. else
  11. if result串的最后一个字符为'零' continue;
  12. else result串的最后一个字符不为'零' result += '零';
  13. else 当前字符是本段最后一个
  14. if 当前字符不为‘0 result += 转换成的繁体字 + 段单位; continue;
  15. else 当前字符为'0'
  16. if result串的最后一个字符 != '零' result += 段单位; continue;
  17. else resutl串的最后一个字符 == '零'
  18. if result串的倒数第二个字符为'亿'或者‘万’ continue;
  19. else result串的倒数第二个字符不为'亿'或者'万' result+=result串去零+段单位;continue;

发表评论

表情:
评论列表 (有 0 条评论,452人围观)

还没有评论,来说两句吧...

相关阅读