Change Attribute to Closest Multiple

jb15jb15 Member Posts: 602
edited November -1 in Working with GS (Mac)
I need to have a random number (say, 100) change to it's closest multiple of another number (say, 32) resulting in 96.

Here's how I've done it so far:

3 attributes:
numberathand (real), 100
integer (integer), 0
multiplier (integer), 32

Behaviors:
Change Attribute "numberathand" to "numberathand / multiplier" ---now our number is 3.125
Change Attribute "integer" to "numberathand" ---now our number is 3
Change Attribute "numberathand" to "integer" x "multiplier" ---now our number is 96.

Is there a better way to do this? I'm not a math person...

Comments

  • RHRH Member Posts: 1,079
    you could just do change attribute numberathand to (floor( self.numberathand/32))*32

    this will snap to the multiple of 32 lower than it currently is. If you replace floor with ceil it will snap to the higher multiple of 32.

    You could stick them both in a rule that says when attribute numberathand is less than ((floor( self.New Attribute /32))*32)+16

    change to ((floor( self.New Attribute /32))*32)

    otherwise

    change to ((ceil( self.New Attribute /32))*32)

    This will change to it whichever is closer between the higher and larger multiple of 32
  • jb15jb15 Member Posts: 602
    Oh wow thanks so much RH! I don't understand everything, but after staring at it a while I'm sure I will. :)
  • RHRH Member Posts: 1,079
    oops, i forgot to change the formula to match yours when i copied and pasted

    swap all the self.newattributes with self.numberathand
Sign In or Register to comment.