the*_*Man 76
Ruby拥有所有标准的打印格式化程序,可通过printf,sprintf或者使用'formatstring' % [var1, ...].
>> '%.2f' % 3.14159 #=> "3.14"
>> '%4s %-4s' % ['foo', 'bar'] #=> " foo bar "
Run Code Online (Sandbox Code Playgroud)
试试这个:
1234567890.123.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
=> "1,234,567,890.123"
Run Code Online (Sandbox Code Playgroud)
取自@pguardiario在类似帖子中的评论