Topic: New Method to roundup an integer to the highest multiple of 10

I haven't written very many of my own methods in Rails but am in need of something that will round up a number to the next highest multiple of 10. I wrote the code which I'm sure is bloated but it works. My problem is, I'm having trouble calling it on an integer in my application.
At first I tried putting it in the application helper and have since moved it to my model and I'm wondering if I'm doing it correctly:

class Tester < ActiveRecord::Base
def self.roundup
if      x.to_i >= 0 && x.to_i <= 10
        z = 10
        return z
elsif x.to_i > 10 && x.to_i <= 20
        z = 20
        return z
elsif x.to_i > 20 && x.to_i <= 30
        z = 30
        return z
else x.to_i > 30 && x.to_i <= 40
        z = 40
        return z
end

I am trying to call my the method like this in my controller:
@x = 8
@x.roundup

I am getting a controller error that says:
undefined method `roundup' for 1:Fixnum

any idears...?
thanks

jackster

Re: New Method to roundup an integer to the highest multiple of 10

You need to create a method for Fixnum, not for Tester:

Add this code to RAILS_ROOT/lib/roundup.rb and then require 'roundup' in your environment.rb:

class Fixnum
  def roundup
    return self if self % 10 == 0   # already a factor of 10
    return self + 10 - (self % 10)  # go to nearest factor 10
  end
end

Example of use:
puts 2.roundup    #=> 10
puts 23.roundup   #=> 30
puts 20.roundup   #=> 20
puts 45.roundup   #=> 50
puts 156.roundup  #=> 160

Re: New Method to roundup an integer to the highest multiple of 10

And if you need it for floats/decimals:

class Float
  def roundup
    return self if self % 10 == 0   # already a factor of 10
    return self + 10 - (self % 10)  # go to nearest factor 10
  end
end

puts 156.34.roundup   #=> 160.0
puts 16.34.roundup    #=> 20.0
puts 81.1234.roundup  #=> 90.0

Re: New Method to roundup an integer to the highest multiple of 10

That's awesome pullmonkey!

Now what if I needed to round up to the nearest 26.4?

class Numeric
def roundup(nearest)
    self % nearest == 0 ? self : self + nearest - (self % nearest)
end
def rounddown(nearest)
    self % nearest == 0 ? self : self - (self % nearest)
end

Defining the method on Numeric gives the method to both Fixnum and Float, as well as Integer.

Last edited by Radar (2008-01-09 22:59:27)

Re: New Method to roundup an integer to the highest multiple of 10

Good to know about Numeric and good call, but maybe nearest should default to 10, so you don't have to pass it in.

class Numeric
  def roundup(nearest=10)
    self % nearest == 0 ? self : self + nearest - (self % nearest)
  end
  def rounddown(nearest=10)
    self % nearest == 0 ? self : self - (self % nearest)
  end
end

Re: New Method to roundup an integer to the highest multiple of 10

you guys are both great coders, that's for sure...and thanks to both of you for helping me solve the problem...but how about a quick lesson for me as to what that code does?

Can we start with "self % nearest" ? I get lost as soon as I see that percent sign :-)

jackster

Re: New Method to roundup an integer to the highest multiple of 10

% means modulus. basically, what's the remainder when you divide the left number by the right number. 5 % 3 will divide cleanly once, and give a remainder of 2.

Re: New Method to roundup an integer to the highest multiple of 10

The % is the modulus operator.  modulus returns the remainder of a division.

So 4 % 10 = 0 + 4/10 = 0 rem 4 which has a remainder of 4.
and 41 % 10 = 4 + 1/10 = 4 rem 1, which has a remainder of 1.

so as far as `self + nearest - (self % nearest)`:

self is the number that is the object.
nearest is probably 10 or whatever is passed in

so if self is 56 and nearest is 10, you get this:
56 + 10 - (56 % 10)
56 % 10 is 5 rem 6 => so 6
56 + 10 - (6) = 56 + 10 - 6 = 60

We have 56 which is 6 higher than the previous factor of 10, so we can add 10 to our number and subtract that distance to get us to the next factor of 10.

Argh, I know that is a bad explanation, hopefully you just somehow get it, let me know otherwise :)

Modulus - http://mathforum.org/library/drmath/view/54363.html  (some lesson, might help)

Last edited by pullmonkey (2008-01-10 00:02:02)

Re: New Method to roundup an integer to the highest multiple of 10

that is an absolutely great example and I understand now....thanks alot!

jackster

Re: New Method to roundup an integer to the highest multiple of 10

Thanks for helping me extend the Fixnum class....that's first for me and I feel pretty good about being able to do that now.


jackster

Re: New Method to roundup an integer to the highest multiple of 10

Hi this is kannn,,,i am new for this site.,
wat i want is how to send the values present in the text field to the php form from flash...plz help me.

Re: New Method to roundup an integer to the highest multiple of 10

open a new thread for your question, and in that new thread, give more detail please.

here you are way off-topic

Re: New Method to roundup an integer to the highest multiple of 10

HI i am having a project of i have to calculate the tiles from the floor designed by us..i finished calculating the tiles..
after tat wat to do is i want to send the tiles nos to the php form.. as the tiles nos are in the variables(input text box)  i can able to send them to the php from flash using post method.. But what my boss wants is at a end of the out put i want the out put movie clip to be converted to a image and have to send them as a image attachment along with the variables to the php .. can you help me.. plz..

Re: New Method to roundup an integer to the highest multiple of 10

1) As i said, open a new thread.
2 This is a Rails forum, not a PHP forum.

thread closed.