New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

fixed_point_field

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fixed_point_field

  • 1.2
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

FixedPointField

Stores floating point values in an integer database column.

Most useful for currency, fixed_point_field is a library that lets you tell your active record classes that some numeric column needs to be upscaled when saved and downscaled when loaded, by some known number of decimal places. It also generates stubs to directly access the fixed point version of the number, if you need to use mathematics and remain exact, you can multiply the fixed width version of your number by another integer and then down convert the result manually. The formula for this is: fixed_num.to_f / (base ** width)

Usage

class Product < ActiveRecord::Base fixed_point_field :price end

prod = Product.new(:price => 12.75) prod.price # => 12.75 prod.price_fixed # => 1275

it is stored as an int

prod.send(:read_attribute, :price) # => 1275

fixed point setter

prod.price_fixed = 1999 prod.price # => 19.99

Other widths and bases

This library was designed to be used to store American currency (USD), but may be useful in other situations as well. To store a number with a fixed point width of 10, use:

fixed_point_field :very_precise_column, :width => 10

You can also store numbers in other bases other than decimal numbers, but any base that is not evenly divisible by 10 will give you rounding errors, negating the value of the plugin. I'm not sure why you'd need this, but here it is:

fixed_point_field :base_twenty_column, :base => 20

Installation

The preferred method of installation is through Rubygems. You can use config.gem, bundler, or manually install the fixed_point_field gem, depending on your version of Rails and desired usage.

Feedback

powerup@rubidine.com

FAQs

Package last updated on 10 Jul 2010

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc