f_decimaltocnf.srf 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. $PBExportHeader$f_decimaltocnf.srf
  2. global type f_decimaltocnf from function_object
  3. end type
  4. forward prototypes
  5. global function string f_decimaltocnf (decimal number, integer cntype)
  6. end prototypes
  7. global function string f_decimaltocnf (decimal number, integer cntype);/*将数字转换为汉字大写的人民币格式或数字格式。
  8. 如:输入 1999.23
  9. 转换为人民币:壹仟玖佰玖拾玖元贰角叁分
  10. 转换为数字为:壹仟玖佰玖拾玖点贰叁
  11. 用法:
  12. 输入数字类型变量:number
  13. 返回字符串: cnstr
  14. 参数整型变量:cntype: =0时,转换为人民币,=1时,转换为数字
  15. **<返回数字不大于18位精确值>
  16. cnstr=f_decimaltocn(number,cntype)
  17. */
  18. string numstr[11]={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖","拾"}
  19. string str1="",str2="",intstr="",decstr=""//整数部分,小数部分,大写整数部分,大写小数部分
  20. string num[]//各个数位段,即每四位为一段
  21. string n[]//各位数字
  22. string number1,number2,number3//定义数字的三个数位段
  23. int pointpos
  24. if cntype=0 then
  25. //如果转换为人民币大写,小数位数只取2位
  26. number=round(number,2)
  27. if isnull(number) then
  28. messagebox("错误","转换为人民币大写的数字整数位数最大17位!")
  29. return ''
  30. end if
  31. end if
  32. pointpos=pos(string(number),".")
  33. if pointpos=0 then//输入值为纯整数
  34. str1=string(number)
  35. str2=""
  36. else
  37. //输入值为浮点数
  38. str1=left(string(number),pointpos -1)//取整数部分
  39. str2=right(string(number),len(string(number)) -pointpos)//取小数部分
  40. end if
  41. if dec(str2)=0 then //如果小数部分为0,则取消小数部分的分析
  42. str2=""
  43. end if
  44. //*****分析转换整数部分*****
  45. choose case len(str1)
  46. case is>8//分析整数部分在100000000以上的
  47. num[1]=left(str1,len(str1) -8)//按每4位为一段拆分成三段,逐段分析
  48. num[2]=left(right(str1,8),4)
  49. num[3]=right(str1,4)
  50. number1=f_decimaltocn(dec(num[1]),1)+"亿"//若干亿
  51. if mid(num[2],1,1)="0" then//分析千万位
  52. n[1]=numstr[1]//若该位为0,则数位赋值“零”
  53. else
  54. n[1]=numstr[integer(mid(num[2],1,1))+1]+"仟"
  55. end if
  56. if mid(num[2],2,1)="0" then//分析百万位
  57. if mid(num[2],1,1)="0" then
  58. n[2]=""//若千万位为0,则该位为空
  59. else
  60. n[2]=numstr[1]
  61. end if
  62. else
  63. n[2]=numstr[integer(mid(num[2],2,1))+1]+"佰"
  64. end if
  65. if mid(num[2],3,1)="0" then//分析十万位
  66. if mid(num[2],2,1)="0" then
  67. n[3]=""
  68. else
  69. n[3]=numstr[1]
  70. end if
  71. else
  72. n[3]=numstr[integer(mid(num[2],3,1))+1] +"拾"
  73. end if
  74. if mid(num[2],4,1)="0" then//分析万位
  75. n[4]=""
  76. else
  77. n[4]=numstr[integer(mid(num[2],4,1))+1]
  78. end if
  79. //
  80. if right(num[2],3)="000" then//当末尾有000时
  81. n[2]=""
  82. n[3]=""
  83. n[4]=""
  84. end if
  85. if right(num[2],2)="00" then//当末尾有00时
  86. n[3]=""
  87. n[4]=""
  88. end if
  89. if right(num[2],1)="0" then//当末尾有0时
  90. n[4]=""
  91. end if
  92. //
  93. if mid(num[3],1,1)="0" then//分析千位
  94. n[5]=numstr[1]
  95. else
  96. n[5]=numstr[integer(mid(num[3],1,1))+1]+"仟"
  97. end if
  98. if mid(num[3],2,1)="0" then//分析百位
  99. if mid(num[3],1,1)="0" then
  100. n[6]=""
  101. else
  102. n[6]=numstr[1]
  103. end if
  104. else
  105. n[6]=numstr[integer(mid(num[3],2,1))+1]+"佰"
  106. end if
  107. if mid(num[3],3,1)="0" then//分析十位
  108. if mid(num[3],2,1)="0" then
  109. n[7]=""
  110. else
  111. n[7]=numstr[1]
  112. end if
  113. else
  114. n[7]=numstr[integer(mid(num[3],3,1))+1] +"拾"
  115. end if
  116. if mid(num[3],4,1)="0" then//分析个位
  117. n[8]=""
  118. else
  119. n[8]=numstr[integer(mid(num[3],4,1))+1]
  120. end if
  121. //
  122. if right(num[3],3)="000" then//当末尾有000时
  123. n[6]=""
  124. n[7]=""
  125. n[8]=""
  126. end if
  127. if right(num[3],2)="00" then//当末尾有00时
  128. n[7]=""
  129. n[8]=""
  130. end if
  131. if right(num[3],1)="0" then//当末尾有0时
  132. n[8]=""
  133. end if
  134. //
  135. //统计
  136. number2=n[1]+n[2]+n[3]+n[4]+"万"
  137. if num[3]="0000" then
  138. number3=""
  139. if num[2]="0000" then
  140. number2=""
  141. end if
  142. else
  143. if num[2]="0000" then
  144. number2=numstr[1]
  145. end if
  146. number3=n[5]+n[6]+n[7]+n[8]
  147. end if
  148. //取得整数位值
  149. intstr=number1+number2+number3
  150. case 5 to 8//分析整数部分在10000~99999999之间的
  151. num[1]=left(str1,len(str1) -4)//取得第一段(千万位到万位)
  152. //不足4位,用'0'补齐
  153. if len(num[1])=3 then
  154. num[1]="0"+num[1]
  155. end if
  156. if len(num[1])=2 then
  157. num[1]="00"+num[1]
  158. end if
  159. if len(num[1])=1 then
  160. num[1]="000"+num[1]
  161. end if
  162. num[2]=right(str1,4)//取得第二段(千位到个位)
  163. number1=""
  164. if mid(num[1],1,1)="0" then//分析千万位
  165. n[1]=""
  166. else
  167. n[1]=numstr[integer(mid(num[1],1,1))+1]+"仟"
  168. end if
  169. if mid(num[1],2,1)="0" then//分析百万位
  170. if mid(num[1],1,1)="0" then
  171. n[2]=""
  172. else
  173. n[2]=numstr[1]
  174. end if
  175. else
  176. n[2]=numstr[integer(mid(num[1],2,1))+1]+"佰"
  177. end if
  178. if mid(num[1],3,1)="0" then//分析十万位
  179. if mid(num[1],2,1)="0" then
  180. n[3]=""
  181. else
  182. n[3]=numstr[1]
  183. end if
  184. else
  185. n[3]=numstr[integer(mid(num[1],3,1))+1] +"拾"
  186. end if
  187. if mid(num[1],4,1)="0" then//分析万位
  188. n[4]=""
  189. else
  190. n[4]=numstr[integer(mid(num[1],4,1))+1]
  191. end if
  192. //
  193. if right(num[1],3)="000" then//当末尾有000时
  194. n[2]=""
  195. n[3]=""
  196. n[4]=""
  197. end if
  198. if right(num[1],2)="00" then//当末尾有00时
  199. n[3]=""
  200. n[4]=""
  201. end if
  202. if right(num[1],1)="0" then//当末尾有0时
  203. n[4]=""
  204. end if
  205. //
  206. if mid(num[2],1,1)="0" then//分析千位
  207. n[5]=numstr[1]
  208. else
  209. n[5]=numstr[integer(mid(num[2],1,1))+1]+"仟"
  210. end if
  211. if mid(num[2],2,1)="0" then//分析百位
  212. if mid(num[2],1,1)="0" then
  213. n[6]=""
  214. else
  215. n[6]=numstr[1]
  216. end if
  217. else
  218. n[6]=numstr[integer(mid(num[2],2,1))+1]+"佰"
  219. end if
  220. if mid(num[2],3,1)="0" then//分析十位
  221. if mid(num[2],2,1)="0" then
  222. n[7]=""
  223. else
  224. n[7]=numstr[1]
  225. end if
  226. else
  227. n[7]=numstr[integer(mid(num[2],3,1))+1] +"拾"
  228. end if
  229. if mid(num[2],4,1)="0" then//分析个位
  230. n[8]=""
  231. else
  232. n[8]=numstr[integer(mid(num[2],4,1))+1]
  233. end if
  234. //
  235. if right(num[2],3)="000" then//当末尾有000时
  236. n[6]=""
  237. n[7]=""
  238. n[8]=""
  239. end if
  240. if right(num[2],2)="00" then//当末尾有00时
  241. n[7]=""
  242. n[8]=""
  243. end if
  244. if right(num[2],1)="0" then//当末尾有0时
  245. n[8]=""
  246. end if
  247. //
  248. //统计
  249. if num[2]="0000" then
  250. number3=""
  251. else
  252. number3=n[5]+n[6]+n[7]+n[8]
  253. end if
  254. number2=n[1]+n[2]+n[3]+n[4]+"万"
  255. //取得整数位值
  256. intstr=number1+number2+number3
  257. case else//分析整数部分不到10000的
  258. num[1]=str1
  259. //不足4位,用'0'补齐
  260. if len(num[1])=3 then
  261. num[1]="0"+num[1]
  262. end if
  263. if len(num[1])=2 then
  264. num[1]="00"+num[1]
  265. end if
  266. if len(num[1])=1 then
  267. num[1]="000"+num[1]
  268. end if
  269. number1=""
  270. number2=""
  271. if mid(num[1],1,1)="0" then//分析千位
  272. n[1]=""
  273. else
  274. n[1]=numstr[integer(mid(num[1],1,1))+1]+"仟"
  275. end if
  276. if mid(num[1],2,1)="0" then//分析百位
  277. if mid(num[1],1,1)="0" then
  278. n[2]=""
  279. else
  280. n[2]=numstr[1]
  281. end if
  282. else
  283. n[2]=numstr[integer(mid(num[1],2,1))+1]+"佰"
  284. end if
  285. if mid(num[1],3,1)="0" then//分析十位
  286. if mid(num[1],2,1)="0" then
  287. n[3]=""
  288. else
  289. n[3]=numstr[1]
  290. end if
  291. else
  292. n[3]=numstr[integer(mid(num[1],3,1))+1] +"拾"
  293. end if
  294. if mid(num[1],4,1)="0" then//分析个位
  295. n[4]=""
  296. else
  297. n[4]=numstr[integer(mid(num[1],4,1))+1]
  298. end if
  299. //
  300. if right(num[1],3)="000" then//当末尾有000时
  301. n[2]=""
  302. n[3]=""
  303. n[4]=""
  304. end if
  305. if right(num[1],2)="00" then//当末尾有00时
  306. n[3]=""
  307. n[4]=""
  308. end if
  309. if right(num[1],1)="0" then//当末尾有0时
  310. n[4]=""
  311. end if
  312. //
  313. //统计
  314. number3=n[1]+n[2]+n[3]+n[4]
  315. ////取得整数位值
  316. intstr=number1+number2+number3
  317. end choose
  318. if str1="0" then//如果整数为零,转换为"零"
  319. intstr=numstr[1]
  320. end if
  321. //*****分析和转换小数部分*****
  322. if cntype=0 then//如果转换为人民币表达式
  323. // if len(str2)>2 then//如果小数位数大于2,就四舍五入.
  324. // str2=right(string(round(double("0"+"."+str2),2)),2)
  325. // end if
  326. if mid(str2,1,1)="0" then
  327. n[1]="零"
  328. else
  329. n[1]=numstr[integer(mid(str2,1,1))+1]+"角"
  330. end if
  331. if mid(str2,2,1)='0' then
  332. n[2]=""
  333. else
  334. n[2]=numstr[integer(mid(str2,2,1))+1]+"分"
  335. end if
  336. decstr=n[1]+n[2]
  337. end if
  338. if cntype=1 then//如果转换为数字表达式
  339. int decpos
  340. for decpos=1 to len(str2)
  341. n[decpos]=numstr[integer(mid(str2,decpos,1))+1]
  342. decstr=decstr+n[decpos]
  343. next
  344. end if
  345. //输出本函数的结果***********************
  346. if cntype=0 then//将数字字串转换为人民币的大写格式
  347. if str2="" then//如果为纯整数
  348. return intstr+"元整"
  349. else
  350. if intstr="零" then//如果整数为零,就只显示小数
  351. return decstr
  352. else
  353. return intstr+"元"+decstr+"整"
  354. end if
  355. end if
  356. end if
  357. if cntype=1 then//将数字字串转换为普通大写格式
  358. if str2="" then//如果为纯整数
  359. return intstr
  360. else
  361. return intstr+"点"+decstr
  362. end if
  363. end if
  364. //完毕OK
  365. end function