Skip to content

Instantly share code, notes, and snippets.

@adamstrickland
Created June 7, 2014 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamstrickland/39a5563b118f50187c35 to your computer and use it in GitHub Desktop.
Save adamstrickland/39a5563b118f50187c35 to your computer and use it in GitHub Desktop.
module Mongoid
class << self
def Money(value)
Money.create_from_value(value)
end
end
class Money
class << self
def create_from_value(value)
self.new(value)
end
end
def initialize(value)
@sign = value.to_f < 0 ? -1 : 1
exp_pos = value.to_s =~ /\./
@exp = if exp_pos.nil?
0
else
value.to_s.size - (exp_pos + 1)
end
@number = (value.to_f * (10**@exp)).to_i
end
def to_f
(@sign * (@number.to_f / (10**@exp))).to_f
end
def precision
@exp
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment