Tuesday, May 29, 2018

Re: money field question

On dinsdag 29 mei 2018 11:03:56 CEST Mike Dewhirst wrote:

 

> Also, despite using decimal.Decimal under the covers it wants its money

> as strings.

>

> Python 3.6.3

>

> >>> from money import money

> >>> print(money.Money(23.45, 'AUD'))

>

> AUD 23.449999999999999289457264239899814128875732421875

>

> >>> print(money.Money('23.45', 'AUD'))

>

> AUD 23.45

 

No, that's decimal.Decimal and floats being an approximation:

 

>>> from decimal import Decimal
>>> Decimal('23.45')
Decimal('23.45')
>>> Decimal(23.45)
Decimal('23.449999999999999289457264239899814128875732421875')

>>> print('{:.48f}'.format(23.45))
23.449999999999999289457264239899814128875732421875
>>> print('{:.55f}'.format(23.45))
23.4499999999999992894572642398998141288757324218750000000

So decimal.Decimal's constructor converts strings to decimal numbers with as much precision as given. It also applies to floats, except that floats have more precision then typed.

--

Melvyn Sopacua

No comments:

Post a Comment