Skip to content

rubidine/fixed_point_field

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Stores floating point values in an integer database column.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages