Author Topic: real to string with math format  (Read 2453 times)

0 Members and 1 Guest are viewing this topic.

dgpuertas

  • Newt
  • Posts: 81
real to string with math format
« on: March 06, 2013, 09:43:34 AM »
I try to convert very big (and small) numbers to string with math format (103, 106, etc )

I Use that function

Code - Auto/Visual Lisp: [Select]
  1. (defun formato_numero_exponente (real ndec / f1 f2)
  2.  
  3.   (defun f1 (z ex nde)
  4.     (if (> z (expt 10. ex))
  5.       (f1 z (+ ex 3) nde)
  6.       (strcat (rtos (/ z (expt 10. (- ex 3))) 2 ndec) "·10\\s\\up4(" (itoa (- ex 3)) ")")))
  7.   (defun f2 (z ex nde)
  8.     (if (< z (expt 10. ex))
  9.       (f2 z (- ex 3) nde)
  10.       (strcat (rtos (/ z (expt 10. (+ ex 3))) 2 ndec) "·10\\s\\up4(" (itoa (+ ex 3)) ")")))
  11.  
  12.   (cond
  13.     ((> real 1000.) (f1 real 3 ndec))
  14.     ((< real 1e-3)  (f2 real -3 ndec))
  15.     (T (rtos real 2 ndec)))
  16.  
  17. )

And works fine

Real its a real number ndec its the number of decimal place.

Code - Auto/Visual Lisp: [Select]
  1. (formato_numero_exponente 230.32 1)
  2. "230.3"
  3. _$ (formato_numero_exponente 1230.32 1)
  4. "1.2·10\\s\\up4(3)"
  5.  

"1.2·10\\s\\up4(3)"
Means 1.2 103
In ms word field EQ

Code - Auto/Visual Lisp: [Select]
  1. _$ (formato_numero_exponente 1232340.32 1)
  2. "1.2·10\\s\\up4(6)"
  3. _$ (formato_numero_exponente 0.123234032 2)
  4. "0.12"
  5. _$ (formato_numero_exponente 0.000123234032 2)
  6. "0.12·10\\s\\up4(-3)"


Any other way??

Thanks and sorry about my english

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: real to string with math format
« Reply #1 on: March 06, 2013, 09:57:30 AM »
Maybe:
Code - Auto/Visual Lisp: [Select]
  1. (defun format ( n d / e )
  2.     (if (< -3 (setq e (fix (/ (log n) (log 10)))) 3)
  3.         (rtos n 2 d)
  4.         (strcat (rtos (/ n (expt 10.0 e)) 2 d) "·10\\s\\up4(" (itoa e) ")")
  5.     )
  6. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (format 1230.32 1)
  2. "1.2·10\\s\\up4(3)"

dgpuertas

  • Newt
  • Posts: 81
Re: real to string with math format
« Reply #2 on: March 06, 2013, 12:56:13 PM »
Much better Lee,

As always your code its great

But I want to do the exponent multiply of 3
3 6 9 12


Thanks
« Last Edit: March 06, 2013, 01:02:00 PM by dgpuertas »

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: real to string with math format
« Reply #3 on: March 07, 2013, 01:20:13 AM »
Oh, so you want engineering format instead of scientific? Similar to the issue I had with Open Office Calc: http://wiki.openoffice.org/wiki/Engineering_and_SI_Number_Format_in_Calc
Using the same principle as I did in that sample ODS file with SBasic:
Code - Auto/Visual Lisp: [Select]
  1. (defun format-eng (num decimals / ex)
  2.   (setq ex 0)
  3.   (while (< num 1000) (setq num (* num 1000) ex (- ex 3)))
  4.   (while (> num 1000) (setq num (/ num 1000) ex (+ ex 3)))
  5.   (apply 'strcat (cons (rtos num 2 decimals) (cond ((/= ex 0) (list "·10\\s\\up4(" (itoa ex) ")"))))))
Seems to work correctly, at least as defined in Engineering Format:
Code: [Select]
_$ (format-eng 1232340.32 1)
"1.2·10\\s\\up4(6)"
_$ (format-eng 0.123234032 2)
"123.23·10\\s\\up4(-3)"
_$ (format-eng 0.000123234032 2)
"123.23·10\\s\\up4(-6)"
_$ (format-eng 123.456 2)
"123.46"
Note officially Eng Format would never allow something like "0.12" as in your 2nd & 3rd examples. If you do need this though the code can be changed to allow for that.
Code - Auto/Visual Lisp: [Select]
  1. (defun format-eng1  (num decimals / ex)
  2.   (setq ex 0)
  3.   (while (< num 0.1) (setq num (* num 1000) ex  (- ex 3)))
  4.   (while (> num 1000) (setq num (/ num 1000) ex  (+ ex 3)))
  5.   (apply 'strcat (cons (rtos num 2 decimals) (cond ((/= ex 0) (list "·10\\s\\up4(" (itoa ex) ")"))))))
Code: [Select]
_$ (format-eng1 0.123234032 2)
"0.12"
_$ (format-eng1 0.000123234032 2)
"0.12·10\\s\\up4(-3)"
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: real to string with math format
« Reply #4 on: March 07, 2013, 06:41:32 AM »
Much better Lee, As always your code its great

Thank you  :-)

But I want to do the exponent multiply of 3
3 6 9 12

Maybe:
Code - Auto/Visual Lisp: [Select]
  1. (defun format ( n d / e x )
  2.    (if (or (zerop n) (< -3 (setq e (fix (/ (log n) (log 10)))) 3))
  3.        (rtos n 2 d)
  4.        (progn
  5.            (if (not (equal 0.0 (rem e 3) 1e-8))
  6.                (if (< e (setq x (* 3 (fix (/ ((if (< e 0) - +) e 1.5) 3)))))
  7.                    (setq e (- x 3))
  8.                    (setq e x)
  9.                )
  10.            )
  11.            (strcat (rtos (/ n (expt 10.0 e)) 2 d) "·10\\s\\up4(" (itoa e) ")")
  12.        )
  13.    )
  14. )
« Last Edit: March 07, 2013, 06:51:11 AM by Lee Mac »

dgpuertas

  • Newt
  • Posts: 81
Re: real to string with math format
« Reply #5 on: March 07, 2013, 09:10:22 AM »
Thanks irneb,
Thanks Lee,

here in theswamp learn a lot.

kirby

  • Newt
  • Posts: 132
Re: real to string with math format
« Reply #6 on: March 08, 2013, 09:16:19 AM »
Pardon my English Density, but how does one use these codes?   I'm using Acad 2011, and it does not support these text formatting codes.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: real to string with math format
« Reply #7 on: March 09, 2013, 12:10:55 AM »
You're correct, it doesn't work for me either (Vanilla 2013). Here's a version which returns a MText formatted string:
Code - Auto/Visual Lisp: [Select]
  1. (defun format-eng  (num decimals / ex)
  2.   (setq ex 0)
  3.   (while (< num 1000) (setq num (* num 1000) ex  (- ex 3)))
  4.   (while (> num 1000) (setq num (/ num 1000) ex  (+ ex 3)))
  5.   (apply 'strcat (list "\\A1;" (rtos num 2 decimals) "·10{\\H0.7x;\\S" (itoa ex) "^;}")))
And here's a sample use of this function:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:Test (/ num en ed)
  2.   (if (and (setq num (getreal "Enter any decimal: "))
  3.            (setq en (entsel "Pick an MText to place the value: ")))
  4.     (if (and (setq ed (entget (car en)))
  5.              (eq (cdr (assoc 0 ed)) "MTEXT"))
  6.       (entmod (subst (cons 1 (format-eng num 2))
  7.                      (assoc 1 ed)
  8.                      (vl-remove-if '(lambda (dxf) (= (car dxf) 40)) ed)))
  9.       (prompt "That is not a MText entity.")))
  10.   (princ))
« Last Edit: March 09, 2013, 12:31:53 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: real to string with math format
« Reply #8 on: March 09, 2013, 12:55:56 AM »
And using the same maths principle as Lee's codes (instead of my 2 while loops)  ;) ... but producing the same format code as mine (i.e. shows the 10^0 = 1 exponent):
Code - Auto/Visual Lisp: [Select]
  1. (defun format-eng2  (num decimals / ex of)
  2.   (setq ex (- (setq ex (fix (/ (log num) (log 10)))) (rem ex 3) (cond ((< ex 0) 3) (t 0))))
  3.   (apply 'strcat (list "\\A1;" (rtos (/ num (expt 10.0 ex)) 2 decimals)
  4.                        "·10{\\H0.7x;\\S" (itoa ex) "^;}")))
For the version the OP wanted (i.e. <1 shown with 0.###) it's even simpler:
Code - Auto/Visual Lisp: [Select]
  1. (defun format-eng3  (num decimals / ex)
  2.   (setq ex (- (setq ex (fix (/ (log num) (log 10)))) (rem ex 3)))
  3.   (apply 'strcat (list "\\A1;" (rtos (/ num (expt 10.0 ex)) 2 decimals)
  4.                        "·10{\\H0.7x;\\S" (itoa ex) "^;}")))
And to re-introduce the idea as per Lee's (i.e. omit the 10^0 exponent):
Code - Auto/Visual Lisp: [Select]
  1. (defun format-eng2a  (num decimals / ex of)
  2.   (setq ex (- (setq ex (fix (/ (log num) (log 10)))) (rem ex 3) (cond ((< ex 0) 3) (t 0))))
  3.   (cond ((zerop ex) (rtos num 2 decimals))
  4.         (t (apply 'strcat (list "\\A1;" (rtos (/ num (expt 10.0 ex)) 2 decimals)
  5.                                 "·10{\\H0.7x;\\S" (itoa ex) "^;}")))))
  6.  
  7. (defun format-eng3a  (num decimals / ex)
  8.   (setq ex (- (setq ex (fix (/ (log num) (log 10)))) (rem ex 3)))
  9.   (cond ((zerop ex) (rtos num 2 decimals))
  10.         (t (apply 'strcat (list "\\A1;" (rtos (/ num (expt 10.0 ex)) 2 decimals)
  11.                                 "·10{\\H0.7x;\\S" (itoa ex) "^;}")))))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: real to string with math format
« Reply #9 on: March 09, 2013, 01:08:25 AM »
Sorry  :-[ stupid of me ... I forgot about negative numbers:
Code - Auto/Visual Lisp: [Select]
  1. (defun format-eng  (num decimals / ex m)
  2.   (setq ex 0 m (cond ((< num 0) "-") (t "")) num (abs num))
  3.   (while (< num 1000) (setq num (* num 1000) ex  (- ex 3)))
  4.   (while (> num 1000) (setq num (/ num 1000) ex  (+ ex 3)))
  5.   (apply 'strcat (list "\\A1;" m (rtos num 2 decimals) "·10{\\H0.7x;\\S" (itoa ex) "^;}")))
  6.  
  7. (defun format-eng1  (num decimals / ex m)
  8.   (setq ex 0 m (cond ((< num 0) "-") (t "")) num (abs num))
  9.   (while (< num 0.1) (setq num (* num 1000) ex  (- ex 3)))
  10.   (while (> num 1000) (setq num (/ num 1000) ex  (+ ex 3)))
  11.   (apply 'strcat (list "\\A1;" m (rtos num 2 decimals) "·10{\\H0.7x;\\S" (itoa ex) "^;}")))
  12.  
  13. (defun format-eng2  (num decimals / ex m)
  14.   (setq m (cond ((< num 0) "-") (t "")) num (abs num)
  15.         ex (- (setq ex (fix (/ (log num) (log 10)))) (rem ex 3) (cond ((< ex 0) 3) (t 0))))
  16.   (apply 'strcat (list "\\A1;" m (rtos (/ num (expt 10.0 ex)) 2 decimals)
  17.                        "·10{\\H0.7x;\\S" (itoa ex) "^;}")))
  18.  
  19. (defun format-eng3  (num decimals / ex m)
  20.   (setq m (cond ((< num 0) "-") (t "")) num (abs num)
  21.         ex (- (setq ex (fix (/ (log num) (log 10)))) (rem ex 3)))
  22.   (apply 'strcat (list "\\A1;" m (rtos (/ num (expt 10.0 ex)) 2 decimals)
  23.                        "·10{\\H0.7x;\\S" (itoa ex) "^;}")))
  24.  
  25. (defun format-eng2a  (num decimals / ex m)
  26.   (setq m (cond ((< num 0) "-") (t "")) num (abs num)
  27.         ex (- (setq ex (fix (/ (log num) (log 10)))) (rem ex 3) (cond ((< ex 0) 3) (t 0))))
  28.   (cond ((zerop ex) (strcat m (rtos num 2 decimals)))
  29.         (t (apply 'strcat (list "\\A1;" m (rtos (/ num (expt 10.0 ex)) 2 decimals)
  30.                                 "·10{\\H0.7x;\\S" (itoa ex) "^;}")))))
  31.  
  32. (defun format-eng3a  (num decimals / ex m)
  33.   (setq m (cond ((< num 0) "-") (t "")) num (abs num)
  34.         ex (- (setq ex (fix (/ (log num) (log 10)))) (rem ex 3)))
  35.   (cond ((zerop ex) (strcat m (rtos num 2 decimals)))
  36.         (t (apply 'strcat (list "\\A1;" m (rtos (/ num (expt 10.0 ex)) 2 decimals)
  37.                                 "·10{\\H0.7x;\\S" (itoa ex) "^;}")))))
Of course I just realized that I could omit the apply idea in these cases  :lmao:
« Last Edit: March 09, 2013, 01:20:04 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: real to string with math format
« Reply #10 on: March 09, 2013, 07:35:38 AM »
Code - Auto/Visual Lisp: [Select]
  1. ex (- (setq ex (fix (/ (log num) (log 10)))) (rem ex 3)))

This expression will produce odd results for some exponents Irné:
Code - Text: [Select]
  1. x               (format x 2)   (format-eng3a x 2)
  2. ---------------------------------------------------
  3. 0.0001234        0.12·10(-3)    0.12·10(-3)
  4. 0.00001234      12.34·10(-6)    0.01·10(-3)
  5. 0.000001234      1.23·10(-6)    0.00·10(-3)
  6. 0.0000001234     0.12·10(-6)    0.12·10(-6)
  7. 0.00000001234   12.34·10(-9)    0.01·10(-6)
  8. 0.000000001234   1.23·10(-9)    0.00·10(-6)

Here is an updated version of mine for use in AutoCAD, and to handle negative numbers:
Code - Auto/Visual Lisp: [Select]
  1. (defun format ( n d / e x )
  2.     (if (or (zerop n) (< -3 (setq e (fix (/ (log (abs n)) (log 10)))) 3))
  3.         (rtos n 2 d)
  4.         (progn
  5.             (if (not (equal 0.0 (rem e 3) 1e-8))
  6.                 (if (< e (setq x (* 3 (fix (/ ((if (< e 0) - +) e 1.5) 3)))))
  7.                     (setq e (- x 3))
  8.                     (setq e x)
  9.                 )
  10.             )
  11.             (strcat "\\A1;" (rtos (/ n (expt 10.0 e)) 2 d) "·10{\\H0.7x;\\S" (itoa e) "^;}")
  12.         )
  13.     )
  14. )

Program to test:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ( / n p )
  2.     (if
  3.         (and
  4.             (setq n (getreal "\nSpecify Number: "))
  5.             (setq p (getpoint "\nSpecify Point for MText: "))
  6.         )
  7.         (entmake
  8.             (list
  9.                '(000 . "MTEXT")
  10.                '(100 . "AcDbEntity")
  11.                '(100 . "AcDbMText")
  12.                 (cons 10 (trans p 1 0))
  13.                 (cons 1 (format n 3))
  14.             )
  15.         )
  16.     )
  17.     (princ)
  18. )

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: real to string with math format
« Reply #11 on: March 09, 2013, 11:32:52 AM »
Code - Auto/Visual Lisp: [Select]
  1. (setq ex (- (setq ex (fix (/ (log num) (log 10)))) (rem ex 3) (cond ((< ex 0) 3) (t 0))))

This expression for the exponent produces good results with the following modification:
Code - Auto/Visual Lisp: [Select]
  1. (defun format ( n d / e )
  2.     (if (or (zerop n) (< -3 (setq e (fix (/ (log (abs n)) (log 10)))) 3))
  3.         (rtos n 2 d)
  4.         (progn
  5.             (if (not (equal 0.0 (rem e 3) 1e-8))
  6.                 (setq e (- e (rem e 3) (if (< e 0) 3 0)))
  7.             )
  8.             (strcat "\\A1;" (rtos (/ n (expt 10.0 e)) 2 d) "·10{\\H0.7x;\\S" (itoa e) "^;}")
  9.         )
  10.     )
  11. )

Good code Irné  :-)