缩短长数以包括K / M / B / T.

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

我想缩短长号,而不包括完整的单位名称:

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

如您所见,没有具体的精确度,但更多的是整体数量的长度

任何人都有一个很好的帮助方法吗?

放入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" 

像这样:

 number_to_human(a_number,format:'%n%u',units:{thousand:'K',million:'M',billion:'B'})