AutoLISP
Developer's Guide > Using the AutoLISP
Language > Using AutoLISP to
Communicate with AutoCAD > Conversions >

String Conversions

*rtos*
(real to string) and *angtos* (angle to string) convert
numeric values used in AutoCAD to string values that can be used in
output or as textual data. The *rtos* function converts a real
value, and *angtos* converts an angle. The
format of the result string is controlled by the value of AutoCAD
system variables: the units and precision are specified by LUNITS
and LUPREC for real (linear) values and by AUNITS and AUPREC for
angular values. For both functions, the dimensioning variable
DIMZIN controls how leading and trailing zeros are written to the
result string.

*rtos* and the values returned
(assuming the DIMZIN system variable equals 0). Precision (the
third argument to *rtos*) is set to 4 places in the
first call and 2 places in the others.

(setq x 17.5)

(setq str "\nValue formatted as ")

(setq fmtval (rtos x 1 4)) ; Mode 1 = scientific

(princ (strcat str fmtval)) ;displaysâ€ƒâ€ƒValue formatted as 1.7500E+01

(setq fmtval (rtos x 2 2)) ; Mode 2 = decimal

(princ (strcat str fmtval)) ;displaysâ€ƒâ€ƒValue formatted as 17.50

(setq fmtval (rtos x 3 2)) ; Mode 3 = engineering

(princ (strcat str fmtval)) ;displaysâ€ƒâ€ƒValue formatted as 1'-5.50"

(setq fmtval (rtos x 4 2)) ; Mode 4 = architectural

(princ (strcat str fmtval)) ;displaysâ€ƒâ€ƒValue formatted as 1'-5 1/2"

(setq fmtval (rtos x 5 2)) ; Mode 5 = fractional

(princ (strcat str fmtval)) ;displaysâ€ƒâ€ƒValue formatted as 17 1/2

*rtos* differs for
engineering (mode equals 3),
architectural (mode equals 4), and
fractional (mode equals 5) units. For
example, the first two lines of the preceding sample output would
be the same, but the last three lines would appear as follows:

Value formatted as 1'5.50"

Value formatted as 1'5-1/2"

Value formatted as 17-1/2''

*angtos* function takes the ANGBASE
system variable into account, the following code always returns
"0":

(angtos (getvar "angbase"))

There is no AutoLISP function that returns a string version (in the current mode/precision) of either the amount of rotation of ANGBASE from true zero (East) or an arbitrary angle in radians.

To find the amount of rotation of ANGBASE from AutoCAD zero (East) or the size of an arbitrary angle, you can do one of the following:

- Add the desired angle to the current ANGBASE, and then check to
see if the absolute value of the result is greater than 2pi; (2 *
pi). If so, subtract 2pi;; if the result is negative, add 2pi;,
then use the
*angtos*function on the result. - Store the value of ANGBASE in a temporary variable, set ANGBASE
to 0, evaluate the
*angtos*function, then set ANGBASE to its original value.

*(atof (angtos 0))* from 360 degrees
(2pi; radians or 400 grads) also yields the rotation of ANGBASE
from 0.

*distof* (distance to floating
point) function is the complement of *rtos*. Therefore, the following
calls, which use the strings generated in the previous examples,
all return the same value: 17.5. (Note the use of the backslash (\)
with modes 3 and 4.)

(distof "1.7500E+01" 1) ; Mode 1 = scientific

(distof "17.50" 2) ; Mode 2 = decimal

(distof "1'-5.50\"" 3) ; Mode 3 = engineering

(distof "1'-5 1/2\"" 4) ; Mode 4 = architectural

(distof "17 1/2" 5) ; Mode 5 = fractional

*angtos* and the values returned
(still assuming that DIMZIN equals 0). Precision (the third
argument to *angtos*) is set to 0 places in the
first call, 4 places in the next three calls, and 2 places in the
last.

(setq ang 3.14159 str2 "\nAngle formatted as ")

(setq fmtval (angtos ang 0 0)) ; Mode 0 = degrees

(princ (strcat str2 fmtval)) ;displaysâ€ƒAngle formatted as 180

(setq fmtval (angtos ang 1 4)) ; Mode 1 = deg/min/sec

(princ (strcat str2 fmtval)) ;displaysâ€ƒAngle formatted as 180d0'0"

(setq fmtval (angtos ang 2 4)) ; Mode 2 = grads

(princ (strcat str2 fmtval)) ;displaysâ€ƒAngle formatted as 200.0000g

(setq fmtval (angtos ang 3 4)) ; Mode 3 = radians

(princ (strcat str2 fmtval)) ;displaysâ€ƒAngle formatted as 3.1416r

(setq fmtval (angtos ang 4 2)) ; Mode 4 = surveyor's

(princ (strcat str2 fmtval)) ;displaysâ€ƒAngle formatted as W

*angtos* when it
returns a string in surveyor's units (mode equals 4). If UNITMODE
equals 0, the string returned can include spaces (for example,
"N 45d E"); if UNITMODE equals 1, the string contains no spaces (for
example, "N45dE").

*angtof* function complements
*angtos*, so all of the following
calls return the same value: 3.14159.

(angtof "180" 0) ; Mode 0 = degrees

(angtof "180d0'0\"" 1) ; Mode 1 = deg/min/sec

(angtof "200.0000g" 2) ; Mode 2 = grads

(angtof "3.14159r" 3) ; Mode 3 = radians

(angtof "W" 4) ; Mode 4 = surveyor's

*angtof* and *distof*
demonstrate this action.