String Conversions
 
 
 

The functions 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.

The following code fragments show calls to 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

When the UNOTHODE system variable is set to 1, specifying that units are displayed as entered, the string returned by 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''

Because the 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:

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

The 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

The following code fragments show similar calls to 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

The UNITHODE system variable also affects strings returned by 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").

The 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 

When you have a string specifying a distance in feet and inches, or an angle in degrees, minutes, and seconds, you must precede the quotation mark with a backslash (\") so it doesn't look like the end of the string. The preceding examples of angtof and distof demonstrate this action.