String to Real Back to String Conversion Dilemma

String to Real Back to String Conversion Dilemma

paullimapa
Mentor Mentor
742 Views
7 Replies
Message 1 of 8

String to Real Back to String Conversion Dilemma

paullimapa
Mentor
Mentor

I'm trying to do a round trip with the following string values converting them to real and then back to string. But I'm having trouble with getting 3 of them to successfully get a round trip.

Here's my code:

(setvar dimzin 0)
; set text string list:
(setq txtlst (list "1/16" "1/20" "1/32" "1/40" "1/48" "1/64"))
; perform text string to real number conversion function:
(setq numlst (mapcar '(lambda (x) (distof x 2)) txtlst))
; AutoCAD correctly returns a unique # for each:
; (0.0625 0.05 0.03125 0.025 0.0208333 0.015625)
; perform real number back to text conversion function:
(setq n2tlst (mapcar '(lambda (x) (rtos x 4 6)) numlst))
; AutoCAD returns with 3 mismatches 1/20, 1/40 & 1/48:
; ("1/16\"" "3/64\"" "1/32\"" "1/32\"" "1/64\"" "1/64\"")

How can I get back "1/20" "1/40" & "1/48" ?

I've tried increasing the precision from 6 to a higher # but that made no difference:

0.05 will not convert back to "1/20"

0.025 will not convert back to "1/40"

0.0208333 will not convert back to "1/48"

 

 

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Accepted solutions (1)
743 Views
7 Replies
Replies (7)
Message 2 of 8

Kent1Cooper
Consultant
Consultant
Accepted solution

(rtos) in Architectural or Fractional mode can return strings with only power-of-2 denominators -- the choices you get in the Precision setting in the UNITS dialog box's Length category:

Kent1Cooper_0-1743899866090.png

If you do this only with the numerator always being 1, as in your examples, something could be worked out to take the inverse of the decimal value, and build the fraction text string with the 1 and the slash and that inverse, rounded off in a lot of cases.  Otherwise, I can imagine too much iffiness about the whole concept of precision if the denominator could be anything.at all.  Suppose you start with "153/1427."  That's going to turn into a decimal value, but how could (rtos) know to dig down that deep for the precision, i.e. how could it know to calculate that numerator which would need to be based on knowing what the denominator is?  What would the precision argument in the function look like?

Kent Cooper, AIA
0 Likes
Message 3 of 8

paullimapa
Mentor
Mentor

Thanks for the feedback...the power-of-2 denominators is definitely the limitation encountered here. 

 


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

Sea-Haven
Mentor
Mentor

Like Kent's suggestion maybe rebuild the string using strcat.

 

: (/ 1. 0.05)
20.0
: (/ 1. 0.0208333)
48.0000768001229
: (/ 1. 0.015625))
64.0

 

0 Likes
Message 5 of 8

paullimapa
Mentor
Mentor

what I ended up doing is since I have these given strings is to look for those real numbers.

So the condition statements when it encounters

0.05 will return "1/20"

0.025 will return "1/40"

0.0208333 will return "1/48"

but for the rest will go through the rtos conversion returning other strings


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

Sea-Haven
Mentor
Mentor

MY $0.05

 

(SETQ NUMLST '(0.0625 0.05 0.03125 0.025 0.0208333 0.015625))
(setq n2tlst (mapcar '(lambda (x)  (STRCAT "1/" (RTOS (/ 1. x) 2 0))) numlst))

("1/16" "1/20" "1/32" "1/40" "1/48" "1/64")

0 Likes
Message 7 of 8

paullimapa
Mentor
Mentor

Thanks for trying but unfortunately the numerator may not always be 1 with the rest of the numbers that need to be accounted for so that would not work


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

Sea-Haven
Mentor
Mentor

@paullimapa yeah woke up this morning and thought what about "3/64"