首页 > 代码库 > ruby 格式化字符串 浮点数

ruby 格式化字符串 浮点数

在ruby中使用format(或 "%")来格式化string.

例子

"Number is %d" % 12     # Number is 12
format("Number is %d" , 12)    # Number is 12

"Number is %d, type  is %s"   %  [12, "cat"]     
# Number is 12, type is cat
format("Number is %d, type  is %s" ,[12, "cat"] )    
# Number is 12, type is cat

"[%10s]"  %  "cat"                #[       cat]
format("[%10s]", "cat")        #[       cat]

"[%-10s]"  %   "cat"              #[cat       ]
format("[%-10s]","cat")        #[cat       ]

原文链接:http://www.dotnetperls.com/format

ruby 格式化字符串 浮点数