views:

62

answers:

3

I am creating a snippet-site where people can rate snippets, just like votes on SO. Currently, when a snippet has over 999 kudos, it looks like on this mug: mug

So what I want is to do a 1K, 1M, etc... kudos like on SO:

-----
|999|
-----

**user does +1**
-----
|1K |
-----

**one million kudos**
-----
|1M |
-----

The same goes for billions, trillions, quadrillions, quintillions, etc... :'D

Has Ruby got methods for this, and if not, how can I write them myself? Thanks.

+1  A: 

I don't think Ruby has something like this. Rails does have a set of number helpers, but doesn't include this specific feature. However, if you view source for "number to human size" you should be able to roll your own without much trouble.

Kevin Sylvestre
I think that returns something in filesizes as far as I remember...
Shyam
A: 

A possible solution is to create an application-wide helper (application_helper.rb inside app/helpers). You can expand this by using nested if's, but I think you get the idea. I am not saying this is the way to do it, but it beats having something working, than nothing at all.

def kudoify(kudos)

if kudos > 1000 && kudos < 1000000 then 
mykudos_prefix = (kudos / 1000 )
mykudos_suffix = (kudos % 1000 )
mykudos = mykudos_prefix.to_s + "K" + mykudos_suffix.to_s
end

return mykudos
end
Shyam
+1  A: 

Looks like it has been implemented. I suggest you have a look at this patch:

https://rails.lighthouseapp.com/projects/8994/tickets/4239-patch-improvements-in-number_helperrb

Shripad K