20 lines
609 B
Ruby
20 lines
609 B
Ruby
module Format; module Currency
|
|
|
|
|
|
# Converts currency units into subunits.
|
|
# @param [String] units
|
|
# @return [Integer]
|
|
def self.dollars_to_cents(units)
|
|
return (units.gsub(',','').gsub(Settings.intntl.currencies[0],'').to_f * 100).to_i
|
|
end
|
|
|
|
# Converts currency subunits into units.
|
|
# @param [Integer] subunits
|
|
# @return [String]
|
|
def self.cents_to_dollars(subunits)
|
|
return (subunits.to_f / 100.0).to_s
|
|
.gsub(/^(\d+)\.0$/, '\1') # remove trailing zero if no decimals (eg. "1.0" -> "1")
|
|
.gsub(/^(\d+)\.(\d)$/, '\1.\20') # add a second zero if single decimal (eg. "9.9" -> "9.90")
|
|
end
|
|
|
|
end; end
|