DigitUpperUtils.java 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. *
  3. * Copyright (c) behosoft Co.,Ltd.
  4. * All Rights Reserved.
  5. *
  6. * This software is the confidential and proprietary information of behosoft.
  7. * (Social Security Department). You shall not disclose such
  8. * Confidential Information and shall use it only in accordance with
  9. * the terms of the license agreement you entered into with behosoft.
  10. *
  11. * Distributable under GNU LGPL license by gnu.org
  12. */
  13. package com.behosoft.util;
  14. /**
  15. * <p>
  16. * Title: behosoft_[子系统统名]_[模块名]
  17. * </p>
  18. * <p>
  19. * Description: [描述该类概要功能介绍]
  20. * </p>
  21. *
  22. * @author behosoft
  23. * @version $Revision$ 2012-5-14
  24. * @author (lastest modification by $Author$)
  25. * @since 1.0
  26. */
  27. public class DigitUpperUtils {
  28. public static final String fraction[] = { "角", "分" };
  29. public static final String digit[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
  30. public static final String unit[][] = { { "元", "万", "亿" }, { "", "拾", "佰", "仟" } };
  31. public static final String sign[] = { "负", "正 " };
  32. /**
  33. * <p>
  34. * Description:[数字金额大写转换,思想先写个完整的然后将如零拾替换成零 要用到正则表达式]
  35. * </p>
  36. *
  37. * @param n
  38. * @return
  39. */
  40. public static String digitUppercase(double n) {
  41. String head = n < 0 ? sign[0] : "";
  42. n = Math.abs(n);
  43. String s = "";
  44. for (int i = 0; i < fraction.length; i++) {
  45. s += (digit[(int) (Math.floor(n * 10 * Math.pow(10, i)) % 10)] + fraction[i]).replaceAll("(零.)+", "");
  46. }
  47. if (s.length() < 1) {
  48. s = "整";
  49. }
  50. int integerPart = (int) Math.floor(n);
  51. for (int i = 0; i < unit[0].length && integerPart > 0; i++) {
  52. String p = "";
  53. for (int j = 0; j < unit[1].length && n > 0; j++) {
  54. p = digit[integerPart % 10] + unit[1][j] + p;
  55. integerPart = integerPart / 10;
  56. }
  57. s = p.replaceAll("(零.)*零$", "").replaceAll("^$", "零") + unit[0][i] + s;
  58. }
  59. return head
  60. + s.replaceAll("(零.)*零元", "元").replaceFirst("(零.)+", "").replaceAll("(零.)+", "零").replaceAll("^整$",
  61. "零元整");
  62. }
  63. public static void main(String[] args) {
  64. String fee = DigitUpperUtils.digitUppercase(1000000D);
  65. System.out.println(fee);
  66. }
  67. }