Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using Float For Currency

7 views
Skip to first unread message

Hunter's Lists

unread,
Dec 12, 2005, 7:05:19 PM12/12/05
to
Howdy,

I have some methods that manipulate floats that represent a currency amount.

I often end up with more precision than I need, i.e.: $9.756.

What is the best way to scale that to 9.76?

Cheers.


men...@rydia.net

unread,
Dec 12, 2005, 7:12:56 PM12/12/05
to

If you're doing anything that matters, don't use floats for
currency. There are a lot of really nasty subtle issues that will
lose money between the cracks.

Usually you want a specialized currency type which uses
fixed-precision arithmetic.

-mental


Andreas S.

unread,
Dec 12, 2005, 7:16:04 PM12/12/05
to

BigDecimal is good.


--
Posted via http://www.ruby-forum.com/.


Hunter's Lists

unread,
Dec 12, 2005, 7:21:17 PM12/12/05
to
Thanks. Fortunately this is just some quick guestimation throw-away stuff.

No real need for precision. I will take a look at BigDecimal but in the
meantime, any thoughts on the original question?

Thx

Wilson Bilkovich

unread,
Dec 12, 2005, 7:22:20 PM12/12/05
to
Try:
val = 9.756
sprintf("%.2f", val)
That says "print 'val' as a floating point value, with two decimal
places of precision.


Neil Stevens

unread,
Dec 12, 2005, 8:33:53 PM12/12/05
to

require 'bigdecimal'

amount = BigDecimal.new('9.756')
rounded = (amount * 100).round / 100
printf('%.02f', rounded)

Outputs '9.76'
--
Neil Stevens - ne...@hakubi.us

'A republic, if you can keep it.' -- Benjamin Franklin

Dave Burt

unread,
Dec 12, 2005, 11:46:44 PM12/12/05
to
"Wilson Bilkovich" <wil...@gmail.com> wrote:
> val = 9.756
> sprintf("%.2f", val)
> That says "print 'val' as a floating point value, with two decimal
> places of precision.

Or just
"%.2f" % val

Cheers,
Dave


Joe Van Dyk

unread,
Dec 13, 2005, 12:09:39 AM12/13/05
to

What cracks can I lose money through?


Stephen Waits

unread,
Dec 13, 2005, 2:05:57 AM12/13/05
to
Joe Van Dyk wrote:
>
> What cracks can I lose money through?

Floating point numbers represent an extremely wide range of values -
much wider than their integer counterparts. This is handled through an
exponent and mantissa. For this ability, they trade off precision.

Think about the case of adding a large floating point number to a small
floating point number:

irb(main):001:0> a = 1.0e30
=> 1.0e+030
irb(main):002:0> b = 1.0e-30
=> 1.0e-030
irb(main):003:0> a + b
=> 1.0e+030

While this is an extreme example, it does demonstrate the loss of
precision. Essentially, in floating point arithmetic we're trying to
squeeze much more out of, say 32 or 48 or 64 bits.

Integer arithmetic, on the other hand, is exact. And therefore so is
fixed point arithmetic; however, fixed point doesn't enjoy the wide
representation range as floats.

The bottom line is you should never use floating point when it comes to
money. Eventually you're going to miss pennies. Instead represent
things in the smallest denomination, such as cents, and fix it up in
presentation, or use a custom Money column type, or data type.

There's your cracks! Just say no... unless you're a hot chick. Even
then it's questionable.

--Steve

Malte Milatz

unread,
Dec 13, 2005, 3:28:57 PM12/13/05
to
Joe Van Dyk:

> What cracks can I lose money through?

irb> 0.2 - 0.05 - 0.15
=> 2.77555756156289e-17

You're actually gaining money here.

Malte

men...@rydia.net

unread,
Dec 13, 2005, 5:33:55 PM12/13/05
to
Quoting Malte Milatz <mal...@gmx-topmail.de>:

Indeed.

The critical take-home lesson:

Floating point arithmetic only approximates arithmetic with real
numbers.

Never write code which assumes that math with floating-point code
will observe the normal laws of arithmetic (most common mistake:
assuming == is useful for testing the equality of two
floating-point results).

If you're unsure, it might be better not to use floating-point at
all.

-mental


mathew

unread,
Dec 16, 2005, 5:10:16 PM12/16/05
to
Joe Van Dyk wrote:
>On 12/12/05, men...@rydia.net <men...@rydia.net> wrote:
>>If you're doing anything that matters, don't use floats for
>>currency. There are a lot of really nasty subtle issues that will
>>lose money between the cracks.
[...]

>
> What cracks can I lose money through?

It's not just that; your program logic can also behave unexpectedly. e.g.

irb> 1.20 - 1.00 == 0.20
=> false

Use BigDecimal for currency. I've just finished some new Rdoc
documentation for it which will hopefully be added to ruby-doc.org and
the 1.9 release, and can forward you a copy if you like.


mathew
--
<URL:http://www.pobox.com/~meta/>
My parents went to the lost kingdom of Hyrule
and all I got was this lousy triforce.

Stephen Waits

unread,
Dec 16, 2005, 5:39:48 PM12/16/05
to
mathew wrote:
> Use BigDecimal for currency. I've just finished some new Rdoc
> documentation for it which will hopefully be added to ruby-doc.org and
> the 1.9 release, and can forward you a copy if you like.

Hi mathew,

Excuse my ignorance, but I'm wondering what BigDecimal does different
than the built-in integer types?

Thanks,
Steve

James Edward Gray II

unread,
Dec 16, 2005, 5:42:58 PM12/16/05
to

It doesn't use floating point arithmetic. That means it is accurate,
but slower.

James Edward Gray II


Eero Saynatkari

unread,
Dec 16, 2005, 5:51:37 PM12/16/05
to

I think this has already been covered, but:

"Money does not float!"

:)

> Cheers.


E

Stephen Waits

unread,
Dec 16, 2005, 5:58:06 PM12/16/05
to

Hi James,

I think you misread my question. I realize the difference between
Integer and FP math, I even explained it earlier in this thread.

I'm asking why one would use BigDecimal, specifically, as opposed to the
built-in Integer types?

Thanks,
Steve

James Edward Gray II

unread,
Dec 16, 2005, 6:01:20 PM12/16/05
to
On Dec 16, 2005, at 4:58 PM, Stephen Waits wrote:

> I think you misread my question.

Sorry about that.

> I'm asking why one would use BigDecimal, specifically, as opposed
> to the built-in Integer types?

Well, BigDecimal lets you work with decimals, but will be slower.
Integers will be faster, but you'll need to handle the conversions,
as needed.

Hope I got it right that time. ;)

James Edward Gray II


Jacob Fugal

unread,
Dec 16, 2005, 6:03:14 PM12/16/05
to

And built-in integer types represent integers, not decimals. :)

That does however, bring up the point of using an integer
representation instead of floating point, eg. manipulating integer
pennies instead of decimal dollars. If you are certain that integer
arithmetic -- with it's corresponding truncation; eg. (5 / 3) * 3
returns 3, not 5 -- is sufficient for your application, it can be
faster than BigDecimal. AFAIK, BigDecimal is doing this same thing
(integer representation), behind the scenes, but is slower due to the
requirements of being flexible in how many decimal places it
represents.

Another class to look into along these lines -- slower but more
encapsulated than a "roll your own" integer representation, and more
domain specific than BigDecimal -- is the great Money library from
Tobias Lütke:

http://dist.leetsoft.com/api/money/

Jacob Fugal


Neil Stevens

unread,
Dec 16, 2005, 6:08:13 PM12/16/05
to
Stephen Waits wrote:
> I'm asking why one would use BigDecimal, specifically, as opposed to the
> built-in Integer types?

One would use BigDecimal instead of Fixnum or Bignum when one is going
to do some operation that might produce a fraction. If you're certain
your operations will always result in whole numbers, though, I don't see
a point in dragging in BigDecimal.

Stephen Waits

unread,
Dec 16, 2005, 6:26:10 PM12/16/05
to
James Edward Gray II wrote:
>
> Well, BigDecimal lets you work with decimals, but will be slower.
> Integers will be faster, but you'll need to handle the conversions, as
> needed.

Ahh, I see what BigDecimal is now. Think I'd rather just use cents and
Integers.

--Steve

0 new messages