Shorten long numbers to include K/M/B/T

后端 未结 2 1495
孤独总比滥情好
孤独总比滥情好 2020-12-14 18:30

I\'ve checked out Rails number_to_human but it\'s not exactly what i want.

I\'d like to shorten long numbers without including the full unit name:

42         


        
相关标签:
2条回答
  • Put in your config/locales/en.yml:

    en:
      number:
        human:
          decimal_units:
            format: "%n%u"
            units:
              unit: ""
              thousand: K
              million: M
              billion: B
              trillion: T
              quadrillion: Q
    

    Then you'll get:

    number_to_human 420 # => "420"
    number_to_human 5680 # => "5.68K"
    number_to_human 12680 # => "12.7K"
    number_to_human 6802251 # => "6.8M"
    number_to_human 894100158 # => "894M"
    
    0 讨论(0)
  • 2020-12-14 19:13

    Like this:

    number_to_human(a_number,format:'%n%u',units:{thousand:'K',million:'M',billion:'B'})
    
    0 讨论(0)
提交回复
热议问题