[LISP] How to convert exact string to number?

[LISP] How to convert exact string to number?

iBlachu
Advocate Advocate
1,670 Views
21 Replies
Message 1 of 22

[LISP] How to convert exact string to number?

iBlachu
Advocate
Advocate

I want to exactly convert a number in a string to a number so that it has the same rounding.

 

An example that doesn't work for me.

 

(cvunit 35.5 "KG" "OZ")

1252.23

 

(rtos (cvunit 35.5 "KG" "OZ") 2 3)

"1252.226"

 

Now how do I convert the string from this "1252.226" calculation to a real number?

 

Such action

(atof (rtos (cvunit 35.5 "KG" "OZ") 2 3))

returns 1252.23, not 1252.226!

0 Likes
Accepted solutions (1)
1,671 Views
21 Replies
Replies (21)
Message 2 of 22

john.uhden
Mentor
Mentor

@iBlachu 

Ii's not that AutoCAD isn't converting correctly.  It's just how it displays the result.

I think I use (read) more than (atof)...

Command: (read "1252.226" )
1252.23

Command: (rtos (read "1252.226") 2 6)
"1252.226000"

Command: dimzin ;; Look it up - controls display of leading and trailing zeroes.

Enter new value for DIMZIN <0>: 8

Command: (rtos (read "1252.226") 2 6)
"1252.226"

John F. Uhden

Message 3 of 22

iBlachu
Advocate
Advocate

Unfortunately, that's not what I'm talking about.

 

Maybe otherwise, why (atof "1252.226") returns 1252.23 and not 1252.226?

 

How to change this rounding?

 

Or even differently, how to replace the string "1252.226" with the number 1252.226?

0 Likes
Message 4 of 22

paullimapa
Mentor
Mentor

as others replied, it's actually there but you don't see it

try: (rtos (atof"1252.226") 2 6)

returns: "1252.226000"

atof precision - Autodesk Community - AutoCAD


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 5 of 22

iBlachu
Advocate
Advocate

I need to have a valid (unrounded) value of 1252.226 in a numeric variable.

How to convert it without rounding?

 

 

0 Likes
Message 6 of 22

paullimapa
Mentor
Mentor

since you want a numeric value I just showed up how...but regardless there has to be a rounding value but that's for you to determine and should not be left as an unknown


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 7 of 22

iBlachu
Advocate
Advocate

So in this case I would like to specify as 3 decimal places.

rtos works and converts to a string.
And I need exactly the same value but as a number.

0 Likes
Message 8 of 22

paullimapa
Mentor
Mentor

again as others have replied to you:

(read(rtos (atof"1252.226") 2 3))


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 9 of 22

iBlachu
Advocate
Advocate

iBlachu_0-1670784585097.png

Unfortunately not.

0 Likes
Message 10 of 22

iBlachu
Advocate
Advocate

The whole problem boils down to this

(setq a 1252.226)

!a

1252.23

 

0 Likes
Message 11 of 22

paullimapa
Mentor
Mentor
Accepted solution

just run the following tests and you'll believe...

Command: (= (setq a(read(rtos (atof"1252.226") 2 3))) 1252.23)
nil

Command: (= (setq a(read(rtos (atof"1252.226") 2 3))) 1252.226)
T

Command: !a
1252.23

Command: (= a 1252.226)
T

Command: (= a 1252.23)

nil


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 12 of 22

iBlachu
Advocate
Advocate

OK, but how do I display it as a number?

0 Likes
Message 13 of 22

paullimapa
Mentor
Mentor

Just display it as a string which you’ll have to covert it to anyways 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 14 of 22

iBlachu
Advocate
Advocate

It's an idea. Thanks for support.

0 Likes
Message 15 of 22

paullimapa
Mentor
Mentor

glad to have helped....cheers!!!


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 16 of 22

Sea-Haven
Mentor
Mentor

I think you were missing the fact that acad holds the number as lots of decimals but various variables control how it is displayed.

 

(setq num 123.456789)
123.456789
: !num
123.456789
: (rtos num 2 3)
"123.457"
(atof (rtos num 2 3))
123.457
: (atof (rtos num 2 2))
123.46
(atof (rtos num 2 5))
123.45679

Correct use of rounding functions will convert the number to what you want as a number.

0 Likes
Message 17 of 22

iBlachu
Advocate
Advocate

It doesn't work in some cases.
Check the case with the number 1252.226

0 Likes
Message 18 of 22

ВeekeeCZ
Consultant
Consultant

So it seems that LISP shows you just 6 valid digits of a real number - while internally holding the maximum precision possible (by definition).

0 Likes
Message 19 of 22

kpblc2000
Advisor
Advisor

 

Hope this code could helps

 

 

(defun t1 (/ value sysvars)
  (setq value   125.226
        sysvars (mapcar (function (lambda (x)
                                    (cons x (getvar x))
                                  ) ;_ end of lambda
                        ) ;_ end of function
                        '("dimzin" "luprec")
                ) ;_ end of mapcar
  ) ;_ end of setq
  (foreach luprec '(0 1 2 4 8)
    (setvar "luprec" luprec)
    (foreach dimzin '(0 1 2 3 4 8 12)
      (setvar "dimzin" dimzin)
      (princ (strcat "\nluprec = "
                     (itoa luprec)
                     "; dimzin = "
                     (itoa dimzin)
                     "; (rtos value 2) = "
                     (rtos value 2)
             ) ;_ end of strcat
      ) ;_ end of princ
    ) ;_ end of foreach
  ) ;_ end of foreach
  (foreach item sysvars
    (setvar (car item) (cdr item))
  ) ;_ end of foreach
  (princ)
) ;_ end of defun

 

 

And results will be like

 

_$ (t1)

luprec = 0; dimzin = 0; (rtos value 2) = 125
luprec = 0; dimzin = 1; (rtos value 2) = 125
luprec = 0; dimzin = 2; (rtos value 2) = 125
luprec = 0; dimzin = 3; (rtos value 2) = 125
luprec = 0; dimzin = 4; (rtos value 2) = 125
luprec = 0; dimzin = 8; (rtos value 2) = 125
luprec = 0; dimzin = 12; (rtos value 2) = 125
luprec = 1; dimzin = 0; (rtos value 2) = 125.2
luprec = 1; dimzin = 1; (rtos value 2) = 125.2
luprec = 1; dimzin = 2; (rtos value 2) = 125.2
luprec = 1; dimzin = 3; (rtos value 2) = 125.2
luprec = 1; dimzin = 4; (rtos value 2) = 125.2
luprec = 1; dimzin = 8; (rtos value 2) = 125.2
luprec = 1; dimzin = 12; (rtos value 2) = 125.2
luprec = 2; dimzin = 0; (rtos value 2) = 125.23
luprec = 2; dimzin = 1; (rtos value 2) = 125.23
luprec = 2; dimzin = 2; (rtos value 2) = 125.23
luprec = 2; dimzin = 3; (rtos value 2) = 125.23
luprec = 2; dimzin = 4; (rtos value 2) = 125.23
luprec = 2; dimzin = 8; (rtos value 2) = 125.23
luprec = 2; dimzin = 12; (rtos value 2) = 125.23
luprec = 4; dimzin = 0; (rtos value 2) = 125.2260
luprec = 4; dimzin = 1; (rtos value 2) = 125.2260
luprec = 4; dimzin = 2; (rtos value 2) = 125.2260
luprec = 4; dimzin = 3; (rtos value 2) = 125.2260
luprec = 4; dimzin = 4; (rtos value 2) = 125.2260
luprec = 4; dimzin = 8; (rtos value 2) = 125.226
luprec = 4; dimzin = 12; (rtos value 2) = 125.226
luprec = 8; dimzin = 0; (rtos value 2) = 125.22600000
luprec = 8; dimzin = 1; (rtos value 2) = 125.22600000
luprec = 8; dimzin = 2; (rtos value 2) = 125.22600000
luprec = 8; dimzin = 3; (rtos value 2) = 125.22600000
luprec = 8; dimzin = 4; (rtos value 2) = 125.22600000
luprec = 8; dimzin = 8; (rtos value 2) = 125.226
luprec = 8; dimzin = 12; (rtos value 2) = 125.226

 

Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям! | Do you find the posts helpful? "LIKE" these posts!
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.


Алексей Кулик aka kpblc | Aleksei Kulik aka kpblc Facebook | LinkedIn
autolisp.ru
Техническая поддержка программистов Autodesk в СНГ
Библиотека пользовательских lisp-функций | Custom Lisp-function library

0 Likes
Message 20 of 22

iBlachu
Advocate
Advocate

Yes, but it's not about displaying the number as a string because it works through rtos, but about displaying the number as a number and here is the problem with this rounding. Your modified example below.

 

(defun t1 (/ value sysvars)
  (setq value   1252.226
        sysvars (mapcar (function (lambda (x)
                                    (cons x (getvar x))
                                  ) ;_ end of lambda
                        ) ;_ end of function
                        '("dimzin" "luprec")
                ) ;_ end of mapcar
  ) ;_ end of setq
  (foreach luprec '(0 1 2 4 8)
    (setvar "luprec" luprec)
    (foreach dimzin '(0 1 2 3 4 8 12)
      (setvar "dimzin" dimzin)
      (princ (strcat "\nluprec = "
                     (itoa luprec)
                     "; dimzin = "
                     (itoa dimzin)
                     "; (rtos value 2) = "
                     (rtos value 2)
             ) ;_ end of strcat
      ) ;_ end of princ


	  (print value)


    ) ;_ end of foreach
  ) ;_ end of foreach
  (foreach item sysvars
    (setvar (car item) (cdr item))
  ) ;_ end of foreach
  (princ)
) ;_ end of defun
0 Likes