# Number Helper method

**URL:** <https://www.ruby-forum.com/t/number-helper-method/136783>\
**Category:** Rails\
**Created:** [May 6, 2008, 12:03pm UTC](https://www.ruby-forum.com/t/number-helper-method/136783 "2008-05-06T12:03:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jags](https://www.ruby-forum.com/u/jags)\
**Post date:** [May 6, 2008, 12:03pm UTC](https://www.ruby-forum.com/t/number-helper-method/136783/1 "2008-05-06T12: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:** ![jags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jags](https://www.ruby-forum.com/u/jags)\
**Post date:** [May 6, 2008, 9:05pm UTC](https://www.ruby-forum.com/t/number-helper-method/136783/2 "2008-05-06T21:05:05Z")

</div>

What about using a helper method:

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

On Tue, May 6, 2008 at 6:03 AM, Jags R.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## jags
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
James M.

---

<div class="post-metadata">

**Author:** ![jags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jags](https://www.ruby-forum.com/u/jags)\
**Post date:** [May 7, 2008, 11:14am UTC](https://www.ruby-forum.com/t/number-helper-method/136783/3 "2008-05-07T11: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 M. wrote:

> What about using a helper method:
> 
> def my\_special\_float\_format(val=0.0)  
> val % 1 == 0 ? val.to\_i : val  
> end
> 
> On Tue, May 6, 2008 at 6:03 AM, Jags R.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > ## jags
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > >
> 
> –  
> James M.

---

<div class="post-metadata">

**Author:** ![jags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jags](https://www.ruby-forum.com/u/jags)\
**Post date:** [May 7, 2008, 2:17pm UTC](https://www.ruby-forum.com/t/number-helper-method/136783/4 "2008-05-07T14:17:21Z")

</div>

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

def to\_spp(num)  
raise ArgumentError unless num.respond\_to?(:floor)  
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  
def to\_spp  
num.floor==num ? num.to\_i.to\_s : num.to\_s  
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:** ![jags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jags](https://www.ruby-forum.com/u/jags)\
**Post date:** [May 7, 2008, 3:21pm UTC](https://www.ruby-forum.com/t/number-helper-method/136783/5 "2008-05-07T15:21:03Z")

</div>

wow! cool!

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