muthu123 Posted May 20, 2010 Posted May 20, 2010 This is my way to convert a float value into a float value with our required decimal points.Is this correct one or we have any alternative way? CODE][/code] (Setq a 1.57215) (atof (rtos a 2 2))[ Quote
MSasu Posted May 20, 2010 Posted May 20, 2010 It is a good solution – AutoLISP doesn't have a built-in ROUND function. Regarsd, Quote
Lee Mac Posted May 20, 2010 Posted May 20, 2010 Nice solution, this is what I normally use: ;; Round (Lee Mac) ;; num ~ number to be rounded. ;; dp ~ decimal place to round at. (defun Round (num dp / fac) (setq fac (expt 10. dp)) (if (<= 0.5 (rem (setq num (* fac num)) 1)) (/ (1+ (fix num)) fac) (/ (fix num) fac) ) ) Quote
Lee Mac Posted May 20, 2010 Posted May 20, 2010 Nice catch! ;; Round (Lee Mac) ;; num ~ number to be rounded. ;; dp ~ decimal place to round at. (defun Round (num dp / fac) (/ (if (<= 0.5 (abs (rem (setq num (* (setq fac (expt 10. dp)) num)) 1))) ((if (minusp num) 1- 1+) (fix num)) (fix num) ) fac ) ) Thanks VovKa Quote
VovKa Posted May 20, 2010 Posted May 20, 2010 (defun vk_RoundReal (InReal Precision) (/ (fix (- (* InReal (setq Precision (expt 10. Precision))) (/ (abs InReal) InReal -2.) ) ) Precision ) ) Quote
Lee Mac Posted May 20, 2010 Posted May 20, 2010 Superb VovKa I love your way of making the half, if you add it, you could make the -2 positive Quote
VovKa Posted May 20, 2010 Posted May 20, 2010 Superb VovKa thanx that was the day of no-ifs programming Quote
Recommended Posts
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.