Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

number_to_human with currency

I am using number_to_human to print 4 Million. I was wondering if there is a method that will add the $ to the front of the number? I run into issues when I have a negative number. If I just throw a $ in front I will get $-4 Million they want -$4 Million

like image 242
LuP Avatar asked Apr 02 '13 18:04

LuP


2 Answers

It's builtin Rails:

number_to_currency(number_to_human(-4000000))
# => "-$4 Million"

The big advantage in using convention is that, when you need i18n, you just have to pass the locale:

number_to_currency(number_to_human(-4000000), locale: 'en-EU')
# => "-4 Million €"
like image 124
apneadiving Avatar answered Oct 05 '22 07:10

apneadiving


Its interesting thing and If It would help to anybody:

How to Present thousands as "K", millions as "M" in ruby for that, 
just follow the steps

Include NumberHelper in controller at top

include ActionView::Helpers::NumberHelper

Use this number_to_human Helper and pass the value, This will convert the value

<%= number_to_human(5223654) %>

Paste this code in en.yml to get it working

en:
  number:
    human:
      decimal_units:
       format: "%n%u"
       units:
         unit: ""
         thousand: K
         million: M
         billion: B
         trillion: T
         quadrillion: Q
like image 28
SSR Avatar answered Oct 05 '22 06:10

SSR