明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
楼主: santalin

[函数] 编写一个函数让数字保留指定整数和小数位数,【不足补零】!

  [复制链接]
发表于 2011-2-12 12:51 | 显示全部楼层
本帖最后由 Gu_xl 于 2011-2-12 13:22 编辑

;;花季20110212
http://hi.baidu.com/123523058/home
  1. (defun C:XS ()
  2.   (XS)
  3. )
  4. (defun xs ()
  5.   (setq a (ssget '((0 . "TEXT"))))
  6.   (setq m (getint "输入小数位数: "))
  7.   (if a
  8.     (progn
  9.       (setq i 0)
  10.       (setq n (sslength a))
  11.       (while (< i n)
  12.         (setq e (ssname a i))
  13.         (setq eb (entget e))
  14.         (setq eb0 (cdr (assoc 1 eb)))
  15.         (setq eb0 (atof eb0))
  16.         (setq eb0 (rtos eb0 2 m))
  17.         (setq eb2 (str_xsws eb0 m))
  18.         (setq eb (subst (cons 1 eb2) (assoc 1 eb) eb))
  19.         (entmod eb)
  20.         (setq i (1+ i))
  21.       )
  22.     )
  23.   )
  24. )

  25. (defun str_xsws        (str xsws / lst xs n bw)
  26.   (if (/= (vl-string-position (ascii ".") str) nil)
  27.     (progn (setq lst (HJ-read->biao str "."))
  28.            (setq xs (cadr lst))
  29.            (setq n (strlen xs))
  30.            (if (< n xsws)
  31.              (progn
  32.                (setq bw (- xsws n))
  33.                (repeat bw
  34.                  (setq xs (strcat xs "0"))
  35.                )
  36.              )
  37.            )
  38.            (setq str (strcat (car lst) "." xs))
  39.     )
  40.     (if        (/= xsws 0)
  41.       (progn
  42.         (setq str (strcat str "."))
  43.         (repeat        xsws
  44.           (setq str (strcat str "0"))
  45.         )
  46.       )
  47.     )
  48.   )
  49.   str
  50. )
  51. (defun HJ-READ->BIAO (str fgf / biao s1 i)
  52.   (setq biao nil)
  53.   (setq i (vl-string-search fgf str))
  54.   (while i
  55.     (setq s1 (substr str 1 i))
  56.     (setq str (substr str (+ 2 i)))
  57.     (setq biao (append biao (list s1)))
  58.     (setq i (vl-string-search fgf str))
  59.   )
  60.   (append biao (list str))
  61. )

发表于 2011-2-12 19:22 | 显示全部楼层
dimzin的值是8的情况下,下面的代码写出的0.3的后面怎么有一堆0?
(setq pt (getpoint "\n写入点:"))
(setq w 0.3)
(setq w (atof (rtos w 2 2)))
(command ".text" "m" pt "" 0 w )
发表于 2011-2-12 20:33 | 显示全部楼层
帮助里不是说了只对rtos有效。你用atof就无效了。
发表于 2011-2-12 22:35 | 显示全部楼层
ZZXXQQ 发表于 2011-2-12 20:33
帮助里不是说了只对rtos有效。你用atof就无效了。

谢谢,一直没往这方面想啊
发表于 2011-2-14 07:33 | 显示全部楼层
谢谢123523058 与Gu_xl ,11楼的程序收藏了,学到了新的定小数位方式。
发表于 2011-2-16 11:28 | 显示全部楼层
学习了,呵呵
发表于 2011-4-13 23:31 | 显示全部楼层
好东西 收藏了
发表于 2011-6-4 16:22 | 显示全部楼层
(defun C:XS ()
  (XS)
)
(defun xs ()
  (setq a (ssget '((0 . "TEXT"))))
  (setq m (getint "输入小数位数: "))
  (if a
    (progn
      (setq i 0)
      (setq n (sslength a))
      (while (< i n)
        (setq e (ssname a i))
        (setq eb (entget e))
        (setq eb0 (cdr (assoc 1 eb)))
        (setq eb0 (atof eb0))
        (setq eb0 (rtos eb0 2 m))
        (setq eb2 (str_xsws eb0 m))
        (setq eb (subst (cons 1 eb2) (assoc 1 eb) eb))
        (entmod eb)
        (setq i (1+ i))
      )
    )
  )
)

(defun str_xsws        (str xsws / lst xs n bw)
  (if (/= (vl-string-position (ascii ".") str) nil)
    (progn (setq lst (HJ-read->biao str "."))
           (setq xs (cadr lst))
           (setq n (strlen xs))
           (if (< n xsws)
             (progn
               (setq bw (- xsws n))
               (repeat bw
                 (setq xs (strcat xs "0"))
               )
             )
           )
           (setq str (strcat (car lst) "." xs))
    )
    (if        (/= xsws 0)
      (progn
        (setq str (strcat str "."))
        (repeat        xsws
          (setq str (strcat str "0"))
        )
      )
    )
  )
  str
)
(defun HJ-READ->BIAO (str fgf / biao s1 i)
  (setq biao nil)
  (setq i (vl-string-search fgf str))
  (while i
    (setq s1 (substr str 1 i))
    (setq str (substr str (+ 2 i)))
    (setq biao (append biao (list s1)))
    (setq i (vl-string-search fgf str))
  )
  (append biao (list str))
)
发表于 2011-7-1 20:52 | 显示全部楼层
有没有批量把2400改为2.4的命令
发表于 2013-1-8 20:38 | 显示全部楼层
Gu_xl 发表于 2011-2-11 12:25
回复 461045462 的帖子

G版,程序怎么不提示选择,而把所有数字小数位数都变了呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-5 06:29 , Processed in 0.208158 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表