Ruby on Rails Sunday, January 30, 2011

For all of you guys who like to work with finance applications:

I have added a release 1.00.01 of the library long-decimal to
rubyforge.org and rubygems.org,
which has been tested for JRuby, Ruby 1.8, Ruby 1.9 and Rubinius.

This adds a type for doing calculations with fixed point numbers:

+, -, *, /
sqrt, cbrt
** (power)
log, exp (also for base 2 and 10)
pi

and quite a few rounding rules, including what is needed for currencies
which do have something
like cent (0.01), but the smallest unit actually occuring is 0.05 or
0.02. This can be supported by
calling

x.round_to_allowed_remainders(2, [0, 5], 10, LongDecimal::ROUND_HALF_UP)

(we want 2 digits after the decimal point and the last digit should be 5
or 10 and we
round to the nearest allowed value, (preferring rounding up if ambiguous).

Installation is done by

gem install long-decimal

Usage:

require "rubygems"
require "long-decimal"

x = LongDecimal("0.080")
y = LongDecimal("37.938")
puts (x+y).to_s
=> "38.018"
put (x-y).to_s
=> "-37.858"
puts (x*y).to_s
=> "3.035040"

Potential future developments:
* adding support for active record to have a better representation of
DECIMAL() from databases.
* adding more functions (sin, cos, tan, cot, sec, csc, asin, acos,...,
sinh, cosh,... asinh, acosh,...)

Any comments are welcome!!

Best regards

Karl

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment