lib/latinum/formatters.rb in latinum-0.2.5 vs lib/latinum/formatters.rb in latinum-0.3.0

- old
+ new

@@ -1,6 +1,6 @@ -# Copyright (c) 2012 Samuel G. D. Williams. <http://www.oriontransfer.co.nz> +# Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com> # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell @@ -30,10 +30,18 @@ end def format(amount) "#{amount.to_s('F')} #{@name}" end + + def to_integral(amount) + amount.to_i + end + + def from_integral(amount) + amount.to_d + end end class DecimalCurrencyFormatter def initialize(options = {}) @symbol = options[:symbol] || '$' @@ -50,11 +58,11 @@ # The sign of the number sign = amount.sign < 0 ? '-' : '' # Decimal places, e.g. the '.00' in '$10.00' - frac = frac[0...2].ljust(@places, @zero) + frac = frac[0...@places].ljust(@places, @zero) # Grouping, e.g. the ',' in '$10,000.00' remainder = fix.size % 3 groups = fix[remainder..-1].scan(/.{3}/).to_a groups.unshift(fix[0...remainder]) if remainder > 0 @@ -66,9 +74,16 @@ "#{whole}#{@separator}#{frac}#{name}" else "#{whole}#{name}" end end + + def to_integral(amount) + (amount * 10**@places).to_i + end + + def from_integral(amount) + (amount.to_d / 10**@places) + end end - end end