See the top rated post in this thread. Click here

Results 1 to 3 of 3

Thread: round to nearest sixteenth

  1. #1
    Member
    Join Date
    2013-03
    Posts
    38
    Login to Give a bone
    0

    Default round to nearest sixteenth

    Is there an easy way to round a given real number to the nearest sixteenth?

    for example

    1.2689 -> 1.25
    1.8845 -> 1.875
    0.998 ->1
    0.4389 ->0.4375

  2. #2
    Administrator rkmcswain's Avatar
    Join Date
    2004-09
    Location
    Earth
    Posts
    9,803
    Login to Give a bone
    2

    Default Re: round to nearest sixteenth

    what about this?

    Code:
    (distof (rtos 1.903234 5 4) 2)
    ;;;
    ;;;
    : (distof (rtos 1.2689 5 4) 2)
    1.25
    : (distof (rtos 1.8845 5 4) 2)
    1.875
    : (distof (rtos 0.998 5 4) 2)
    1.0
    : (distof (rtos 0.4389 5 4) 2)
    0.4375
    R.K. McSwain | CAD Panacea |

  3. #3
    Member
    Join Date
    2013-03
    Posts
    38
    Login to Give a bone
    0

    Default Re: round to nearest sixteenth

    Quote Originally Posted by rkmcswain View Post
    what about this?

    Code:
    (distof (rtos 1.903234 5 4) 2)
    ;;;
    ;;;
    : (distof (rtos 1.2689 5 4) 2)
    1.25
    : (distof (rtos 1.8845 5 4) 2)
    1.875
    : (distof (rtos 0.998 5 4) 2)
    1.0
    : (distof (rtos 0.4389 5 4) 2)
    0.4375
    Thank you. I have to read up on 'distof' and 'rtos' to understand what you posted. I came up with something below using Lee Mac subroutines

    Code:
    ;; Round Down  -  Lee Mac
    ;; Rounds 'n' down to the nearest 'm'
    
    
    (defun LM:rounddown ( n m )
        ((lambda ( r ) (cond ((equal 0.0 r 1e-8) n) ((< n 0) (- n r m)) ((- n r)))) (rem n m))
    )
    
    
    ;; Round Up  -  Lee Mac
    ;; Rounds 'n' up to the nearest 'm'
    
    
    (defun LM:roundup ( n m )
        ((lambda ( r ) (cond ((equal 0.0 r 1e-8) n) ((< n 0) (- n r)) ((+ n (- m r))))) (rem n m))
    )
    
    
    (defun AS:round16th (x)
          (setq d1 (- (LM:roundup x 0.0625) x))
          (setq d2 (- x (LM:rounddown x 0.0625)))
           (cond ((> d1 d2) (LM:rounddown x 0.0625))
             (T (LM:roundup x 0.0625))
           )
    )
    
    
    (setq y (AS:round16th 0.468))
    Edit:
    After playing around with your code it works great (and a hell of a lot simpler!!)
    Thanks
    Last edited by Frank Dux; 2019-01-22 at 01:26 PM.

Similar Threads

  1. NEArest Parameter
    By clshade in forum AutoCAD LT - Wish List
    Replies: 0
    Last Post: 2012-03-04, 07:42 PM
  2. Connecting Round Flex Duct To A Round Hard Vertical Duct
    By elaforge in forum Revit MEP - General
    Replies: 6
    Last Post: 2008-02-06, 08:31 PM
  3. Round up to the nearest even number
    By ReachAndre in forum AutoLISP
    Replies: 2
    Last Post: 2007-04-16, 02:22 PM
  4. Rounding up to the nearest 5
    By ReachAndre in forum AutoLISP
    Replies: 7
    Last Post: 2007-01-29, 06:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •