马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
本帖最后由 newer 于 2017-8-18 12:51 编辑
- ;|;
- 功 能 字符串用指定字符首位补位
- 参 数 intVal:要补位的数或字符串。;
- before: 数,小数位前面的位数, 字符串,当前字符串前面的个数(包括当前);
- sp 补位字符;
- after 小数位数,对字符串,当前字符串后面的字符个数
- 示 例 (XD::String:Strpad 123 4 "0" 0)
- 注 意 以上示例将返回“0123”。
- |;
- (defun XD::String:Strpad (intval before sp after / nstr str1 val1 zeros)
- (xdrx_sysvar_push '(("Dimzin" 0)))
- (if (or (= (type intval) 'INT) (= (type intval) 'REAL))
- (progn
- (setq str1 (xdrx_math_modf intval))
- (setq val1 (fix (car str1)))
- (setq nstr (strcat (apply 'strcat
- (repeat (- before (xdrx_string_length (itoa val1)))
- (setq zeros (cons sp zeros))
- )
- )
- (itoa val1)
- )
- )
- (cond ((> after 0)
- (setq nstr (strcat nstr
- "."
- (car (xdrx_string_regexps
- "(?<=\\.)[0-9]+"
- (if (= (cadr str1) 0.0)
- "0.0"
- (rtos (cadr str1) 2 after)
- )
- )
- )
- )
- )
- )
- ((< after 0)
- (if (= (type intval) 'REAL)
- (setq nstr (strcat nstr
- "."
- (car (xdrx_string_regexps
- "(?<=\\.)[0-9]+"
- (vl-princ-to-string (cadr str1))
- )
- )
- )
- )
- )
- )
- )
- )
- (progn (setq nstr (strcat (apply 'strcat
- (repeat (- before (xdrx_string_length intval))
- (setq zeros (cons sp zeros))
- )
- )
- intval
- )
- )
- (setq zeros nil)
- (setq nstr
- (strcat
- nstr
- (apply 'strcat (repeat after (setq zeros (cons sp zeros))))
- )
- )
- )
- )
- (xdrx_sysvar_pop)
- nstr
- )
_$ (XD::String:Strpad 234.94 5 "0" 3)
"234.940"
_$ (XD::String:Strpad 234.94 6 "0" 4)
"234.9400"
_$ (XD::String:Strpad 234.94 12 "0" 4)
"0000234.9400"
_$ (XD::String:zeropad 234 12 "0" 4)
"0000234.0000"
_$ (XD::String:Strpad 234 12 "0" 0)
"000000000234"
_$ (XD::String:Strpad 0 12 "0" 0)
"000000000000"
_$ (XD::String:Strpad 0.0 3 "0" 0)
"000"
_$ (XD::String:Strpad 0.0 3 "0" 4)
"0.0000"
_$
|