2009/01/29

comma

# 2009-01-29.
comma.rb

def comma(str)
  str = str.to_s
  s, res = str.size, []
  k = (s - 4) % 3
  ary = str[0..str.size].each_char.map{|i| i}
  ary.each_index{|i|
  res << ary[i]
  (i == k and i + 1 < s) ? (k, res = k + 3, res << ",") : k
  }
  # Japanese EN => \u{A5} 
  return "\u{A5} " + res.join('')
end
str = "100"
p comma(str)
str = "1001"
p comma(str)
str = "10011"
p comma(str)
str = "100001"
p comma(str)
str = "1001100"
p comma(str)

% ./comma.rb
=>
"¥ 100"
"¥ 1,001"
"¥ 10,011"
"¥ 100,001"
"¥ 1,001,100"


memo
daybook/2008/12/1228746600.html

--imported_from
http://www.midore.net/daybook/2009/01/1233232901.html

0 件のコメント: