缩短长数字以包括 K/M/B/T [英] Shorten long numbers to include K/M/B/T

查看:55
本文介绍了缩短长数字以包括 K/M/B/T的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经检查了 Rails number_to_human 但这不是我想要的.

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:

420 -> 420
5,680 -> 5,680
12,680 -> 12.6K
6,802,251 -> 6.80M
894,100,158 -> 894M

如您所见,没有特定的精度,但更多的是关于总数的长度

As you see there is no specific precision but it's more about the length of the overall number

有没有人有这方面的好帮手方法?

Any one has a good helper method for this?

推荐答案

放入你的config/locales/en.yml:

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

然后你会得到:

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"

这篇关于缩短长数字以包括 K/M/B/T的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆