Does Ruby have any number formatting classes?

后端 未结 4 862
我在风中等你
我在风中等你 2020-12-18 17:53

Does Ruby have any Formatter classes or methods that can be used to format numbers for things like currency, etc., or are there any gems that do this, or do you have to writ

相关标签:
4条回答
  • 2020-12-18 18:08

    You can use Kernel#sprintf (or Kernel#format) and do it that way. API Link.

    0 讨论(0)
  • 2020-12-18 18:11

    Try this:

    1234567890.123.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
    => "1,234,567,890.123"
    

    Taken from a comment by @pguardiario in a similar thread

    0 讨论(0)
  • 2020-12-18 18:18

    Ruby has all the standard print formatters, available either via printf, sprintf or using 'formatstring' % [var1, ...].

    >> '%.2f' % 3.14159 #=> "3.14"
    >> '%4s %-4s' % ['foo', 'bar'] #=> " foo bar "
    
    0 讨论(0)
  • 2020-12-18 18:19

    You can check out the ruby on rails ActionView::Helpers::NumberHelper

    0 讨论(0)
提交回复
热议问题