簡體   English   中英

是否有一個寶石可以縮短像Twitter這樣的追隨者?

[英]Is there a gem to shorten large numbers like Twitter does for followers?

例如:

  • 410589實際顯示為410K
  • 4509實際上顯示為4K
  • 349實際顯示為349

我正在尋找一種方法來將非常大的數字縮短為一種易於閱讀的格式。

number_to_human非常接近你所要求的。 它向上和向下舍入,所以410589將成為“411 K”,不確定是否可以。

你需要一些I18n映射,或提供一個單位的散列來獲得你的后綴,並稍微調整一下以獲得你想要的精度,但是例如:

opts = { units: { thousand: "K" }, precision: 0, significant: false }

number_to_human 410589, opts  # => "411 K"
number_to_human 4509, opts    # => "5 K"
number_to_human 349, opts     # => "349"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM