# Number Helper method

**URL:** <https://discuss.rubyonrails.org/t/number-helper-method/28999>\
**Category:** rubyonrails-talk\
**Created:** [May 6, 2008, 10:03am UTC](https://discuss.rubyonrails.org/t/number-helper-method/28999 "2008-05-06T10:03:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [May 6, 2008, 10:03am UTC](https://discuss.rubyonrails.org/t/number-helper-method/28999/1 "2008-05-06T10:03:15Z")

</div>

hello all

i was lookin for a method which wud print the floating numbers without fractional part if the fractional part is 0

5.0 shud print 5 5.5 shud print 5.5

is there any helper method to do it

jags

---

<div class="post-metadata">

**Author:** ![James\_Mitchell1](https://avatars.discourse-cdn.com/v4/letter/j/bbce88/32.png) [@James\_Mitchell1](https://discuss.rubyonrails.org/u/James_Mitchell1)\
**Post date:** [May 6, 2008, 7:04pm UTC](https://discuss.rubyonrails.org/t/number-helper-method/28999/2 "2008-05-06T19:04:03Z")

</div>

What about using a helper method:

def my\_special\_float\_format(val=0.0) val % 1 == 0 ? val.to\_i : val end

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [May 7, 2008, 9:14am UTC](https://discuss.rubyonrails.org/t/number-helper-method/28999/3 "2008-05-07T09:14:19Z")

</div>

Hi James

Thanx for that

further on this suppose the method name is sff(special float format) how will i able to do such statement anyfloatvalue.to\_spp like we do anyfloatvalue.to\_i

i dont want to pass argument

thanks again

James Mitchell wrote:

---

<div class="post-metadata">

**Author:** ![AndyV](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@AndyV](https://discuss.rubyonrails.org/u/AndyV)\
**Post date:** [May 7, 2008, 12:16pm UTC](https://discuss.rubyonrails.org/t/number-helper-method/28999/4 "2008-05-07T12:16:04Z")

</div>

I'd probably go with something a bit more specific to a float:

def to\_spp(num) &nbsp;&nbsp;raise ArgumentError unless num.respond\_to?(:floor) &nbsp;&nbsp;num.floor==num ? num.to\_i.to\_s : num.to\_s end

Personally I would recommend putting this in application\_helper.rb and using it like the 'number\_to\_currency' helper because it appears to be something that you only want to do when rendering the value in a view.

If you want to use it as an instance method on float (as you suggest) then it's fairly straightforward. Ruby classes are always open for extension so you just open the class and add it. Often extensions to core classes are put in /lib/core\_ext with a file name similar to the class your extending. In this case you might have the following in / lib/core\_ext/float\_ext.rb

class Float &nbsp;&nbsp;def to\_spp &nbsp;&nbsp;&nbsp;&nbsp;num.floor==num ? num.to\_i.to\_s : num.to\_s &nbsp;&nbsp;end end

Then in your environment.rb you'd require 'core\_ext/float\_ext' (it'll search and find the path under lib) and you could simply invoke it on your floats.

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [May 7, 2008, 1:21pm UTC](https://discuss.rubyonrails.org/t/number-helper-method/28999/5 "2008-05-07T13:21:03Z")

</div>

wow! cool!

thanks andy thats all i needed.got some ruby insight in the process
