Probably not the answer you expect: You could make two steps one after the other.
In Ruby: s.gsub('.','').gsub(',','.')
This action could be integrated in a CSV-parser.
Example:
require 'csv'
data = <<data
Saldo contabile:,"1.123.456,89"
Saldo disponibile:,"1.123.456,89"
Saldo disponibile:,"-1.123.456,89"
data
CSV::Converters[:num_it] = ->(s) {(s =~ /[-\d\.,]+/) ? (s.gsub('.','').gsub(',','.').to_f) : s}
#Alternative version with String#tr
#CSV::Converters[:num_it] = ->(s) {(s =~ /[-\d\.,]+/) ? (s.tr('.,', '_.').to_f) : s}
csv = CSV(data, :headers => false, :col_sep => ',',
#~ :converters => :all
:converters => [
*CSV::Converters.keys,
:num_it
]
)
csv.each do |row|
#~ p row
print row[0];print " %.2f\n" % row[1]
end
Result:
Saldo contabile: 1123456.89
Saldo disponibile: 1123456.89
Saldo disponibile: -1123456.89
Beside the version with two sub
, you could also try one String#gsub
with the hash or block-version:
s = "1.123.456,89"
p s.gsub(/[\.,]/, {'.' => ',', ',' => '.'}) #-> "1,123,456.89"
p s.gsub(/[\.,]/){|hit| hit == '.' ? ',' : '.'} #-> "1,123,456.89"