# Rounding problem

**URL:** <https://www.ruby-forum.com/t/rounding-problem/186004>\
**Category:** Rails\
**Created:** [March 30, 2010, 10:57am UTC](https://www.ruby-forum.com/t/rounding-problem/186004 "2010-03-30T10:57:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![JanneKo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3e96dc/32.png) [@JanneKo](https://www.ruby-forum.com/u/JanneKo)\
**Post date:** [March 30, 2010, 10:57am UTC](https://www.ruby-forum.com/t/rounding-problem/186004/1 "2010-03-30T10:57:22Z")

</div>

Hi,

I have a strange problem with number rounding:  
If I try “a\_number.round(2)” in console - it works, but the same line  
thru passenger fails: “wrong number of arguments (1 for 0)”.  
Any ideas why?

I’m running rails 2.3.4 and passenger 2.2.9

---

<div class="post-metadata">

**Author:** ![JanneKo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3e96dc/32.png) [@JanneKo](https://www.ruby-forum.com/u/JanneKo)\
**Post date:** [March 30, 2010, 11:21am UTC](https://www.ruby-forum.com/t/rounding-problem/186004/2 "2010-03-30T11:21:22Z")

</div>

On Tue, Mar 30, 2010 at 1:21 AM, JanneKo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi,
> 
> I have a strange problem with number rounding:  
> If I try “a\_number.round(2)” in console - it works, but the same line  
> thru passenger fails: “wrong number of arguments (1 for 0)”.  
> Any ideas why?
> 
> I’m running rails 2.3.4 and passenger 2.2.9

Hi, what’s the value of a\_number being used?

-Conrad

---

<div class="post-metadata">

**Author:** ![JanneKo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3e96dc/32.png) [@JanneKo](https://www.ruby-forum.com/u/JanneKo)\
**Post date:** [March 30, 2010, 11:31am UTC](https://www.ruby-forum.com/t/rounding-problem/186004/3 "2010-03-30T11:31:19Z")

</div>

On Mar 30, 9:21 am, JanneKo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> I have a strange problem with number rounding:  
> If I try “a\_number.round(2)” in console - it works, but the same line  
> thru passenger fails: “wrong number of arguments (1 for 0)”.  
> Any ideas why?

Is a\_number an integer ?  
rails overrides round to allow it to take a precision argument, but it  
only does that on Float - Integers still have the standard round  
method, which does not take a precision argument.

Fred

---

<div class="post-metadata">

**Author:** ![JanneKo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3e96dc/32.png) [@JanneKo](https://www.ruby-forum.com/u/JanneKo)\
**Post date:** [March 30, 2010, 12:00pm UTC](https://www.ruby-forum.com/t/rounding-problem/186004/4 "2010-03-30T12:00:00Z")

</div>

Thanks for the answers - This solved my problem partly. In database  
(mysql) the number is a decimal - but for some reason it seems to be  
an integer in rails if it’s decimals are zeros.

Janne

On 30 maalis, 12:30, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![JanneKo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3e96dc/32.png) [@JanneKo](https://www.ruby-forum.com/u/JanneKo)\
**Post date:** [March 30, 2010, 4:44pm UTC](https://www.ruby-forum.com/t/rounding-problem/186004/5 "2010-03-30T16:44:34Z")

</div>

class Float  
def round\_to(x)  
(self \* 10**x).round.to\_f / 10**x  
end  
end

then you can use round\_to(2)

Sincerely,

Joel D.  
Website Bio: [http://jdezenzio.com/](http://jdezenzio.com/)  
Rails Production Sites: [http://ncaastatpages.com](http://ncaastatpages.com)

---

<div class="post-metadata">

**Author:** ![JanneKo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3e96dc/32.png) [@JanneKo](https://www.ruby-forum.com/u/JanneKo)\
**Post date:** [March 30, 2010, 8:11pm UTC](https://www.ruby-forum.com/t/rounding-problem/186004/6 "2010-03-30T20:11:15Z")

</div>

what is the range of values that you’re working with? 0 to 100, -3000 to  
4000?

On 30 March 2010 04:21, JanneKo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You received this message because you are subscribed to the Google G.  
> “Ruby on Rails: Talk” group.  
> To post to this group, send email to removed\_email\_address@domain.invalid.  
> To unsubscribe from this group, send email to  
> removed\_email\_address@domain.invalid[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> .  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
Charles A. Lopez  
removed\_email\_address@domain.invalid

What’s your vision for your organization?  
What’s your biggest challenge?

Let’s talk.  
(IBM Partner)

---

<div class="post-metadata">

**Author:** ![JanneKo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3e96dc/32.png) [@JanneKo](https://www.ruby-forum.com/u/JanneKo)\
**Post date:** [March 30, 2010, 6:05pm UTC](https://www.ruby-forum.com/t/rounding-problem/186004/7 "2010-03-30T18:05:23Z")

</div>

On 30 March 2010 15:40, Joel D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Float  
> def round\_to(x)  
> (self \* 10**x).round.to\_f / 10**x  
> end  
> end

To avoid floating point errors, you might as well just do :  
(self \* 1.0).round.(x)  
or  
self.to\_f.round.(x)

And wouldn’t that be a patch to class Integer, as Float already has  
.round working fine? Or am I missing something ?:-/
