[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Using Float For Currency

Hunter's Lists

12/13/2005 12:05:00 AM

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.




13 Answers

MenTaLguY

12/13/2005 12:13:00 AM

0

Quoting Hunter's Lists <lists@lastonepicked.com>:

> 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?

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.

12/13/2005 12:16:00 AM

0

unknown wrote:
> Quoting Hunter's Lists <lists@lastonepicked.com>:
>
>> 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?
>
> 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.

BigDecimal is good.


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


Hunter's Lists

12/13/2005 12:21:00 AM

0

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

> From: <mental@rydia.net>
> Reply-To: <ruby-talk@ruby-lang.org>
> Date: Tue, 13 Dec 2005 09:12:56 +0900
> To: ruby-talk ML <ruby-talk@ruby-lang.org>
> Subject: Re: Using Float For Currency
>
> Quoting Hunter's Lists <lists@lastonepicked.com>:
>
>> 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?
>
> 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
>




Wilson Bilkovich

12/13/2005 12:22:00 AM

0

On 12/12/05, Hunter's Lists <lists@lastonepicked.com> wrote:
> 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?
>
Try:
val = 9.756
sprintf("%.2f", val)
That says "print 'val' as a floating point value, with two decimal
places of precision.


Neil Stevens

12/13/2005 1:34:00 AM

0

Hunter's Lists wrote:
> 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?

require 'bigdecimal'

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

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

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

Dave Burt

12/13/2005 4:47:00 AM

0

"Wilson Bilkovich" <wilsonb@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


Dave Burt

12/13/2005 4:47:00 AM

0

"Wilson Bilkovich" <wilsonb@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

12/13/2005 5:10:00 AM

0

On 12/12/05, mental@rydia.net <mental@rydia.net> wrote:
> Quoting Hunter's Lists <lists@lastonepicked.com>:
>
> > 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?
>
> 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.

What cracks can I lose money through?


Joe Van Dyk

12/13/2005 5:10:00 AM

0

On 12/12/05, mental@rydia.net <mental@rydia.net> wrote:
> Quoting Hunter's Lists <lists@lastonepicked.com>:
>
> > 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?
>
> 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.

What cracks can I lose money through?


Stephen Waits

12/13/2005 7:06:00 AM

0

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