# Atof

## Recommended Posts

Hi all,

I have a problem while converting the string to real number.

my string is like "5644.55688442" when i use (atof "5644.55688442") it returning only 4 points after period like 5644.5568.

help me how can i fix this.

Thanks,

Kumar.

##### Share on other sites

the accuracy is still there, it's just not displayed

checked with this

```\$ (atof "5644.55688442")
5644.56
_\$ (rtos (atof "5644.55688442") 2
"5644.55688442"
```

##### Share on other sites

Thanks a lot for quick reply

##### Share on other sites

You can check that atof always returns a real with 6 digits, regardless of what is the precision of returned number:

```(atof "123456789"); returns 1.2345[color=red]7[/color]e+008
(atof "12345678.9"); returns 1.2345[color=red]7[/color]e+007
(atof "1234567.89"); returns 1.2345[color=red]7[/color]e+006
(atof "123456.789"); returns 12345[color=red]7[/color].0
(atof "12345.6789"); returns 12345.[color=red]7[/color]
(atof "1234.56789"); returns 1234.5[color=red]7[/color]
(atof "123.456789"); returns 123.45[color=red]7[/color]
(atof "12.3456789"); returns 12.3457
(atof "1.23456789"); returns 1.2345[color=red]7[/color]
(atof "0123456789"); returns 1.2345[color=red]7[/color]e+008
```

Howerver, the real precision (accuracy) of real numbers in AutoLISP is 16 digits.

to get the number as desired precision, use rtos with atof:

```(rtos (atof "1.234567890123456789") 2 0); returns "1"
(rtos (atof "1.234567890123456789") 2 1); returns "1.2"
(rtos (atof "1.234567890123456789") 2 2); returns "1.23"
(rtos (atof "1.234567890123456789") 2 3); returns "1.23[color=red]5[/color]"
(rtos (atof "1.234567890123456789") 2 4); returns "1.234[color=red]6[/color]"
(rtos (atof "1.234567890123456789") 2 5); returns "1.2345[color=red]7[/color]"
(rtos (atof "1.234567890123456789") 2 6); returns "1.23456[color=red]8[/color]"
(rtos (atof "1.234567890123456789") 2 7); returns "1.234567[color=red]9[/color]"
(rtos (atof "1.234567890123456789") 2 ; returns "1.23456789"
(rtos (atof "1.234567890123456789") 2 9); returns "1.23456789"
(rtos (atof "1.234567890123456789") 2 10); returns "1.2345678901"
(rtos (atof "1.234567890123456789") 2 11); returns "1.23456789012"
(rtos (atof "1.234567890123456789") 2 12); returns "1.234567890123"
(rtos (atof "1.234567890123456789") 2 13); returns "1.234567890123[color=red]5[/color]"
(rtos (atof "1.234567890123456789") 2 14); returns "1.2345678901234[color=red]6[/color]"
(rtos (atof "1.234567890123456789") 2 15); returns "1.23456789012345[color=red]7[/color]"
(rtos (atof "1.234567890123456789") 2 16); returns [b]"1.23456789012345[color=red]6[/color]" [color=red]<--[/color][/b]
(rtos (atof "1.234567890123456789") 2 17); returns [b]"1.23456789012345[color=red]6[/color]" [color=red]<--[/color][/b]
```

##### Share on other sites

Also, be sure to note that the precision is 16 digits, not decimal places, as the following code demonstrates:

```(defun test ( n / i a b ) (setq i -1 a "")
(while
(/=
(strlen (setq b (rtos n 2 (setq i (1+ i)))))
(strlen a)
)
(print (setq a b))
)
(princ
(strcat "\n\nMaximum Precision: "
(itoa
(- (strlen b) (cond ( (vl-string-position 46 b) ) ( (1- (strlen b)) )) 1)
)
" d.p."
)
)
(princ)
)
```

```_\$ (test 1.12345678911234567891) ;; 20 d.p, 21 digits

"1"
"1.1"
"1.12"
"1.123"
"1.1235"
"1.12346"
"1.123457"
"1.1234568"
"1.12345679"
"1.123456789"
"1.1234567891"
"1.12345678911"
"1.123456789112"
"1.1234567891123"
"1.12345678911235"
"1.123456789112346"

Maximum Precision: 15 d.p.
```

```_\$ (test 123.12345678911234567891) ;; 20 d.p, 23 digits

"123"
"123.1"
"123.12"
"123.123"
"123.1235"
"123.12346"
"123.123457"
"123.1234568"
"123.12345679"
"123.123456789"
"123.1234567891"
"123.12345678911"
"123.123456789112"
"123.1234567891123"

Maximum Precision: 13 d.p.
```

```_\$ (test 1234567891.12345678911234567891) ;; 20 d.p, 30 digits

"1234567891"
"1234567891.1"
"1234567891.12"
"1234567891.123"
"1234567891.1235"
"1234567891.12346"
"1234567891.123457"

Maximum Precision: 6 d.p.
```

Hence for a Real with integer part of length 16 digits:

```_\$ (test 1234567891123456.12345678911234567891) ;; 20 d.p, 36 digits

"1234567891123456"

Maximum Precision: 0 d.p.
```

##### Share on other sites

Hi All,

I have a question about the cad rounding proccess.

For example;

I created a line from command-line but huge coordinate (15digit.15digit ex. Start coor.:(123456789012345.123456789012345,0).

After that, I use the filter command and explore to "filter.nfl" file, autocad return the start coordinate 123456789012345.156250000000000. I hope rounding value "~.123460" but value is "~.156250"

Why?

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.