add mathematical operators for Value to operate with Numeric objects

This commit is contained in:
josh committed 2013-08-13 22:08:23 -04:00
1 parent 31d89371ff
commit d7475fb09e
2 files changed
+84 -4

No files matched your search

+38 -4
View File
@@ -35,14 +35,48 @@ module Gnucash
end
end
# Add two Value objects
# Add to a Value object
# +other+ can be another Value or a Numeric
def +(other)
Value.new(@val + other.val)
if other.is_a?(Value)
Value.new(@val + other.val)
elsif other.is_a?(Numeric)
(to_f + other).round(2)
else
raise "Unexpected argument"
end
end
# Subtract two Value objects
# Subtract from a Value object
# +other+ can be another Value or a Numeric
def -(other)
Value.new(@val - other.val)
if other.is_a?(Value)
Value.new(@val - other.val)
elsif other.is_a?(Numeric)
(to_f - other).round(2)
else
raise "Unexpected argument"
end
end
# Multiply a Value object
# +other+ should be a Numeric
def *(other)
if other.is_a?(Numeric)
(to_f * other).round(2)
else
raise "Unexpected argument"
end
end
# Divide a Value object
# +other+ should be a Numeric
def /(other)
if other.is_a?(Numeric)
(to_f / other).round(2)
else
raise "Unexpected argument"
end
end
# Represent the Value as a string (two decimal places)