uCommon.cpp
上传用户:liuxiao
上传日期:2022-07-06
资源大小:1115k
文件大小:3k
源码类别:

打印编程

开发平台:

C++ Builder

  1. //---------------------------------------------------------------------------
  2. #pragma hdrstop
  3. #include "uCommon.h"
  4. //---------------------------------------------------------------------------
  5. #include "math.h"
  6. // 常量字符串定义,如修改也可变为转换普通汉字函数(如:一二三,十百千)
  7. const char *HanDigiStr[] = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
  8. const char *HanDiviStr[] = { "","拾","佰","仟","万","拾","佰","仟","亿",
  9. "拾","佰","仟","万","拾","佰","仟","亿",
  10. "拾","佰","仟","万","拾","佰","仟" };
  11. AnsiString PositiveIntegerToHanStr(String NumStr)
  12. {// 输入字符串必须正整数,只允许前导空格(必须右对齐),不宜有前导零
  13. AnsiString RMBStr;
  14. int len,n,lastzero=0;
  15. int hasvalue=0;// 亿、万进位前有数值标记
  16. len = NumStr.Length();
  17. if( len > 24 ) return "数值过大!";
  18. for(int i=len-1;i>=0;i--) {
  19. if( NumStr[len-i]==' ' ) continue;      // AnsiString[] base is 1
  20. n = NumStr[len-i] - '0';
  21. if( n<0 || n>9 ) return "输入含非数字字符!";
  22. if( n!=0 ) {
  23. if( lastzero ) RMBStr += HanDigiStr[0];// 若干零后若跟非零值,只显示一个零
  24.                                                                         // 除了亿万前的零不带到后面
  25. //if( !( n==1 && (i%4)==1 && (lastzero || i==len-1) ) )   // 如十进位前有零也不发壹音用此行
  26. //if( !( n==1 && (i%4)==1 && i==len-1 ) )         // 十进位处于第一位不发壹音
  27. RMBStr += HanDigiStr[n];
  28. RMBStr += HanDiviStr[i];// 非零值后加进位,个位为空
  29. hasvalue = 1;                                   // 置万进位前有值标记
  30.  }
  31. else {
  32. if( (i%8)==0 || ((i%8)==4 && hasvalue) ) // 亿万之间必须有非零值方显示万
  33. RMBStr += HanDiviStr[i];// “亿”或“万”
  34.  }
  35.                 if( i%8==0 ) hasvalue = 0;  // 万进位前有值标记逢亿复位
  36. lastzero = (n==0) && (i%4!=0);                  // 亿万前有零后不加零,如:拾万贰仟
  37.  }
  38. if( RMBStr.Length()==0 ) return HanDigiStr[0];        // 输入空字符或"0",返回"零"
  39. return RMBStr;
  40. }
  41. AnsiString NumToRMBStr(double val)
  42. {
  43. AnsiString SignStr,TailStr;
  44. double fraction, integer;
  45. int jiao,fen;
  46. if( val<0 ) {
  47. val = -val;
  48. SignStr = "负";
  49.  }
  50. fraction = modf(val+0.005, &integer);           // 四舍五入到分
  51. jiao = int(fraction*10);
  52. fen = int(fraction*100)-jiao*10;
  53. if( jiao==0 && fen==0 ) {
  54. TailStr = "整";
  55.  }
  56. else {
  57. TailStr = HanDigiStr[jiao];
  58. if( jiao!=0 )
  59. TailStr += "角";
  60.                 if( integer==0 && jiao==0 )             // 零元后不写零几分
  61.                         TailStr = "";
  62. if( fen!=0 )
  63. TailStr += HanDigiStr[fen] + AnsiString("分");
  64.  }
  65. // 下一行可用于非正规金融场合,0.03只显示“叁分”而不是“零元叁分”
  66. //        if( integer==0 ) return  SignStr+TailStr;
  67. return SignStr+PositiveIntegerToHanStr(String(integer))+"元"+TailStr;
  68. }
  69. //-----------------------------------------------------------
  70. #pragma package(smart_init)