使用Mongoid的货币数据类型

时间:2022-10-31 16:32:17

I guess floats are not ideal for currency. Mongoid supports Float and BigInteger. What's the best approach for storing and working with currency values?

我猜浮标不适合货币。 Mongoid支持Float和BigInteger。存储和使用货币值的最佳方法是什么?

3 个解决方案

#1


6  

You might probably want to have a look at the Money gem.

你可能想看一下Money gem。

The way it works, is to represent money amounts in cents and use integers. You can follow this way and store your data as integer so that you don't need to deal with Float precision.

它的工作方式是以美分表示金额并使用整数。您可以按照这种方式将数据存储为整数,这样就不需要处理Float精度。

#2


1  

What Simone Says.

西蒙娜说的话。

I just inserted the money gem in my project and you can store it as a Money type as well.

我只是在我的项目中插入了money gem,你也可以将它存储为Money类型。

class Product
  include Mongoid::Document

  field :price,    type: Money
end

Money.class_eval do

  # Converts an object of this instance into a database friendly value.
  def mongoize
    [cents, currency.to_s]
  end

  class << self

    # Get the object as it was stored in the database, and instantiate
    # this custom class from it.
    def demongoize(object)
      cur = object[1] || Money.default_currency
      Money.new(object[0], cur)
    end

    # Takes any possible object and converts it to how it would be
    # stored in the database.
    def mongoize(object)
      case object
      when Money
        object.mongoize
      else object
      end
    end

    # Converts the object that was supplied to a criteria and converts it
    # into a database friendly form.
    def evolve(object)
      case object
      when Money then object.mongoize
      else object
      end
    end
  end

end   

#3


0  

Floats would work fine for currency if you didn't actually use the fraction part, that is, if you only stored prescaled integral values. Floats store integers and perform integer ops exactly.

如果您实际上没有使用小数部分,也就是说,如果您只存储了预分频积分值,则浮点数对于货币可以正常工作。 Floats存储整数并完全执行整数运算。

Of course, at that point, you might as well use integers.

当然,在那一点上,你不妨使用整数。

#1


6  

You might probably want to have a look at the Money gem.

你可能想看一下Money gem。

The way it works, is to represent money amounts in cents and use integers. You can follow this way and store your data as integer so that you don't need to deal with Float precision.

它的工作方式是以美分表示金额并使用整数。您可以按照这种方式将数据存储为整数,这样就不需要处理Float精度。

#2


1  

What Simone Says.

西蒙娜说的话。

I just inserted the money gem in my project and you can store it as a Money type as well.

我只是在我的项目中插入了money gem,你也可以将它存储为Money类型。

class Product
  include Mongoid::Document

  field :price,    type: Money
end

Money.class_eval do

  # Converts an object of this instance into a database friendly value.
  def mongoize
    [cents, currency.to_s]
  end

  class << self

    # Get the object as it was stored in the database, and instantiate
    # this custom class from it.
    def demongoize(object)
      cur = object[1] || Money.default_currency
      Money.new(object[0], cur)
    end

    # Takes any possible object and converts it to how it would be
    # stored in the database.
    def mongoize(object)
      case object
      when Money
        object.mongoize
      else object
      end
    end

    # Converts the object that was supplied to a criteria and converts it
    # into a database friendly form.
    def evolve(object)
      case object
      when Money then object.mongoize
      else object
      end
    end
  end

end   

#3


0  

Floats would work fine for currency if you didn't actually use the fraction part, that is, if you only stored prescaled integral values. Floats store integers and perform integer ops exactly.

如果您实际上没有使用小数部分,也就是说,如果您只存储了预分频积分值,则浮点数对于货币可以正常工作。 Floats存储整数并完全执行整数运算。

Of course, at that point, you might as well use integers.

当然,在那一点上,你不妨使用整数。