ruby怎么格式化数字

Python022

ruby怎么格式化数字,第1张

比如:

floatnum=‘126,456’

floatnum = floatnum.gsub(/,/,'')#第一参数是要匹配的字符串或字符,第二个参数是要替换的字符串或字符,这里按你的意思,应该是替换成空的

puts floatnum

123456

好了 首先是第一步转成16进制: icon = "22 12 41 2 1 8 43 211 42"# =>"22 12 41 2 1 8 43 211 42" icon.split.map {|i| i.to_i.to_s(16) }# =>["16", "c", "29", "2", "1", "8", "2b", "d3", "2a"] 然后是你说的格式化(不太明白你的意思,不知道是不是个位数前面要补0的意思): icon.split.map {|i| i.to_i.to_s(16) }.map { |s| s.size == 1 ? s.insert(0, "0") : s}# =>["16", "0c", "29", "02", "01", "08", "2b", "d3", "2a"] 最后拼起来: icon.split.map {|i| i.to_i.to_s(16) }.map { |s| s.size == 1 ? s.insert(0, "0") : s}.join=>"160c290201082bd32a"

print "line 1: "line1 = STDIN.gets.chomp()

print "line 2: "line2 = STDIN.gets.chomp()

print "line 3: "line3 = STDIN.gets.chomp()

puts "I'm going to write these to the file."

target.write( [ line1 ,line2 ,line3 ,'' ].join( '\n ' ) )