
Back to AutoLisp Intermediate Tutorial Back to AutoLisp Tutorial Homepage
|
|
The AutoLisp Intermediate Tutorial Number Functions abs atof atoi fix float itoa Example Program abs - This function returns the absolute value of a number. Syntax : (abs number) number - any valid number.[integer or real number] Returns an integer or a real number. (abs 345) returns 345 (abs 345.5) returns 345.5 (abs -345) returns 345 (abs -345.5) returns 345.5 (abs "345JEFF") returns "Error: Bad Argument Type" ["345JEFF" is a string, not a number]
atoi - This function converts a string to a integer. Syntax : (atoi "string") "string" - any valid string or variable representing a string. If you use a variable omit the quotes. Returns an integer. (atoi "Jeff345") returns 0 (atoi "5") returns 5 (atoi "5.6") returns 5 (atoi "") returns 0 (atoi "345JEFF") returns 3 (atoi 5) returns "Error: Bad argument type". [5 is an integer, not a string] (atoi 5.6) returns "Error: Bad argument type". [5.6 is a real number, not a string] This function looks at the first character, then the next, then the next, ect. until it finds a character that cannot be part of an integer. itoa - This function converts an integer to a string. Syntax : (itoa integer) integer - Any valid integer. Returns a string. (itoa 345) returns "345" (itoa 5) returns "5" (itoa 5.6) returns "Error: Bad argument type". [5.6 is a real number, not an integer] (itoa "5") returns "Error: Bad argument type". ["5" is a string not an integer] (itoa "345JEFF") returns "Error: Bad argument type". ["345JEFF" is a string, not an integer] This function can only accept an integer or a variable representing an integer as it's parameter. atof - This function converts a string to real. Syntax : (atof "string") "string" - any valid string or variable representing a string. If you use a variable omit the quotes. Returns an real number. (atof "Jeff345") returns 0.0 (atof "5") returns 5.0 (atof "5.6") returns 5.6 (atof "5'3-1/2"") returns 63.5 (atof "3-1/2"") returns 3.5 (atof "") returns 0.0 (atof "345JEFF") returns 345.0 (atof 345) returns "Error: Bad Argument Type" [345 is an integer, not a string] (atof 3.4) returns "Error: Bad Argument Type" [345 is a real number, not a string] (atof (list 3 4)) returns "Error: Bad Argument Type" [(list 3 4) is a list, not a string] This function looks at the first character, then the next, then the next, ect. until it finds a character that cannot be part of an real number. fix - This function converts a real to an integer. Syntax : (fix real) real - Any valid real number. Returns an integer. (fix 345.0) returns 345 (fix 5.0) returns 5 (fix 5.6) returns 5 (fix "5.0") returns "Error: Bad Argument Type ["5.0" is a string, not a real number] (fix "5") returns "Error: Bad Argument Type ["5" is a string, not a real number] This function takes the whole number part of a decimal number and returns it as an integer.
float - This function takes a number (integer or real) and converts it to a real number. Syntax : (float integer) (float real) Integer - Any valid integer. real - Any valid real number. (float 5) returns 5.0 (float 345) returns 345.0 (float 3.5) returns 3.5 [No effect, but no error] (float "3") returns "Error: Bad Argument Type" ["3" is a string, not a number] (float "3.5") returns "Error: Bad Argument Type" ["3.5" is a string, not a number] (float "abc") returns "Error: Bad Argument Type" ["abc" is a string, not a number] Example Program 1: (defun C:myProg() (setq intAge(getint "\n Enter your Age: ")) (setq intDays(* intAge 365)) (princ (strcat "\n You are " (itoa intDays) " Days old!")) (princ) ) Execution: Command: (load "myProg")<enter> Command: myProg<enter> Command: Enter your Age: 39<enter> Command: You are 14235 Days old! AutoLisp Intermediate Tutorial All questions/complaints/suggestions should be sent to JefferyPSanders.com Last Updated September 28th, 2008 Copyright 2002-2008 JefferyPSanders.com. All rights reserved. |