On May 3, 9:03 pm, “Michael W. Ryder” [email protected]
wrote:
require ‘enumerator’
dan
Your method is much better than my C style one. With a little work to
handle exceptions it should generally work. The only problem I have
found so far is that it doesn’t handle periods in the number string
properly – i.e. sform(“12345.67”, “$00,000.00”) returns $12,345…6"
instead of $12,345.67". Something for me to work on. Thanks for the code.
I made some changes to handle the decimal point
and the case when there are fewer digits in the
string than in the format.
The part on the left of the decimal point.
def sform_left( str, fmt )
result = ‘’
fmt = fmt.split(//)
str.split(//).reverse_each{|d|
while fmt.last != ‘0’ do
result = fmt.pop + result
end
fmt.pop
result = d + result
}
result = fmt.first + result if fmt.first != ‘0’
result
end
The part on the right of the decimal point.
def sform_right( str, fmt )
ary = str.split(//)
fmt.gsub( /0/ ){ ary.shift || ‘0’ }
end
def sform( str, fmt )
str = str.split(‘.’)
fmt = fmt.split(‘.’)
result = sform_left( str[0], fmt[0])
if fmt[1]
result += “.” + sform_right( str.last, fmt.last)
end
result
end
puts sform(“12345”, “$00,000”)
puts sform(“1234”, “$00,000”)
puts sform(“123”, “$00,000”)
puts sform(“12345.6”, “$00,000.00”)
puts sform(“12345.678”, “$00,000.00”)
puts sform(“12345.678”, “$00,000”)
— output —
$12,345
$1,234
$123
$12,345.60
$12,345.67
$12,345