Results 1 to 8 of 8

Thread: Rounding up to the nearest 5

  1. #1
    I could stop if I wanted to
    Join Date
    2015-12
    Posts
    385
    Login to Give a bone
    0

    Default Rounding up to the nearest 5

    Hey all,
    I wrote a basic lisp so that I can calculate CFM. I woudl like this to round up to the nearest 5. Ex: 31.55 will equal 35. Hope this is descriptive enough. Though I am asking about ho wto do that, any help simplifying my code would be appreciated.
    Thank you all in advance,
    Andre

    Code:
    ;Set CFM per Square Foot
    (defun c:cfmsf ()
      (setq cfmpersqft (getreal "\enter CFM per sq ft number: "))
      (princ (strcat "CFM per sq ft set to " cfmpersqft " "))
      (princ))
    
    ;Placing CFM tag
    (defun c:cfm (/ currlay sqft)
      (command "undo" "begin")
      (setq currlay (getvar "Clayer"))
      (setq sqft (getreal "\nenter room square foot: "))
      (setvar "clayer" "M-QUCO-CHCK")
      (command ".text" pause "9" 0 (strcat (rtos (* sqft cfmpersqft)) " CFM" ""))
      (setvar "clayer" currlay)
      (command "undo" "end")
      (princ))
    Last edited by Opie; 2007-01-26 at 03:07 PM. Reason: [CODE] tags added

  2. #2
    Certifiable AUGI Addict
    Join Date
    2001-03
    Location
    Tallahassee, FL USA
    Posts
    3,667
    Login to Give a bone
    0

    Default Re: Rounding up to the nearest 5

    Here's an old routine that works very well:
    Code:
    ;;; Written by Doug Broad
    ;;; If value given 'to' argument is a real, a real is returned.
    
    (defun round (value to)
      (setq to (abs to))
      (* to (fix (/ ((if (minusp value) - +) value (* to 0.5)) to)))
    )
    
    ; examples:
    ; (round 12232e-015 1)
    ; 0
    ; (round 12232e-015 1.0)
    ; 0.0
    ; (round 123.6 1.0)
    ; 124.0
    ; (round -123.6 1.0)
    ; -124.0
    ; (round 6.00000000008 4)
    ; 8
    ; 6.00000000008  rounded to the nearest multiple of 4 is 8

  3. #3
    AUGI Addict kennet.sjoberg's Avatar
    Join Date
    2002-05
    Posts
    1,707
    Login to Give a bone
    0

    Default Re: Rounding up to the nearest 5

    Quote Originally Posted by ReachAndre
    . . . round up to the nearest 5. Ex: 31.55 will equal 35.. .
    Like this ?
    Code:
    (defun 5up ( value / )
      (float (+ (* 5 (/ (fix value) 5 )) 5 ))
    )
    (5up 31.55 ) => 35.0
    (5up 37.55 ) => 40.0
    (5up 0.55 ) => 5.0
    (5up 4.55 ) => 5.0
    (5up 0 ) => 5.0 ?
    (5up 30 ) => 35.0 ?

    : ) Happy Computing !

    kennet

  4. #4
    Past Vice President / AUGI Volunteer peter's Avatar
    Join Date
    2000-09
    Location
    Honolulu HI
    Posts
    1,109
    Login to Give a bone
    0

    Default Re: Rounding up to the nearest 5

    Kennet I noticed that 5up will turn 5.0 to 10.0

    maybe

    (defun RoundUp (sngValue)(* (fix (/ (+ sngValue 4.9999999999999999) 2)) 2.0))

    Peter

  5. #5
    AUGI Addict kennet.sjoberg's Avatar
    Join Date
    2002-05
    Posts
    1,707
    Login to Give a bone
    0

    Default Re: Rounding up to the nearest 5

    Quote Originally Posted by peter
    Kennet I noticed that 5up will turn 5.0 to 10.0
    . . Yes, I believe that that is still the mission. Or ?

    : ) Happy Computing !

    kennet

  6. #6
    Certifiable AUGI Addict
    Join Date
    2001-03
    Location
    Tallahassee, FL USA
    Posts
    3,667
    Login to Give a bone
    0

    Default Re: Rounding up to the nearest 5

    Quote Originally Posted by ReachAndre
    Hey all,
    I wrote a basic lisp so that I can calculate CFM. I woudl like this to round up to the nearest 5. Ex: 31.55 will equal 35. Hope this is descriptive enough.
    ;roundup modifyed from round.
    (defun roundup (value to)
    (setq to (abs to))
    (* to (fix (/ ((if (minusp value) - +) value (1+(* to 0.5))) to)))
    )

    ; (roundup 31.55 5)
    ; 35
    Which was the origional request.

    If you want more decimal places...
    ; (roundup 31.55 5.0)
    ; 35.0
    ; (roundup 31.55 5.00)
    ; 35.00

  7. #7
    AUGI Addict kennet.sjoberg's Avatar
    Join Date
    2002-05
    Posts
    1,707
    Login to Give a bone
    0

    Default Re: Rounding up to the nearest 5

    Quote Originally Posted by Tom Beauford
    ; (roundup 31.55 5)
    ; 35
    Which was the origional request.
    Yes, but where ? is the breakpoint
    ; (roundup 30.99 5) = 30
    ; (roundup 30.01 5) = 30
    ; (roundup 31.01 5) = 30
    ; (roundup 31.55 5) = 35

    I think OP have to clarify. . .otherwise my code always step up to next n*5

    : ) Happy Computing !

    kennet

  8. #8
    Certifiable AUGI Addict
    Join Date
    2001-03
    Location
    Tallahassee, FL USA
    Posts
    3,667
    Login to Give a bone
    0

    Default Re: Rounding up to the nearest 5

    Quote Originally Posted by kennet.sjoberg
    Yes, but where ? is the breakpoint
    ; (roundup 30.99 5) = 30
    ; (roundup 30.01 5) = 30
    ; (roundup 31.01 5) = 30
    ; (roundup 31.55 5) = 35

    I think OP have to clarify. . .otherwise my code always step up to next n*5

    : ) Happy Computing !

    kennet
    One more try:
    Code:
     (defun roundup (value to)
      (setq to (abs to)
    		  try (+(* to (fix (/ ((if (minusp value) - +) value (* to 0.5)) to)))to)
      )
      (if(eq (+ 0.1 try) (+ 0.1 value to))(- try to) try)
    )
    Tested this one better, I went with 35=35 though.

Similar Threads

  1. 2013: Rounding to nearest .5 in schedules
    By jplatte in forum Revit MEP - General
    Replies: 8
    Last Post: 2013-04-08, 06:30 PM
  2. Rounding up, down or to nearest... any interval
    By DoTheBIM in forum Revit Architecture - General
    Replies: 11
    Last Post: 2013-03-20, 03:07 PM
  3. NEArest Parameter
    By clshade in forum AutoCAD LT - Wish List
    Replies: 0
    Last Post: 2012-03-04, 07:42 PM
  4. Rounding to the nearest integer in Tables
    By kkinchen in forum AutoCAD Tables
    Replies: 1
    Last Post: 2007-04-26, 04:23 PM
  5. Round up to the nearest even number
    By ReachAndre in forum AutoLISP
    Replies: 2
    Last Post: 2007-04-16, 02:22 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
  •