flotantes

Walter Gomez waltergomez en ic2k.com.ar
Sab Abr 29 01:09:18 CEST 2006


Existe una libreria en http://fixedpoint.sourceforge.net que te soluciona el 
problema.

Simple

FixedPoint objects support decimal arithmetic with a fixed number of
digits (called the object's precision) after the decimal point.  The
number of digits before the decimal point is variable & unbounded.

The precision is user-settable on a per-object basis when a FixedPoint
is constructed, and may vary across FixedPoint objects.  The precision
may also be changed after construction via FixedPoint.set_precision(p).
Note that if the precision of a FixedPoint is reduced via set_precision,
information may be lost to rounding.

>>> x = FixedPoint("5.55")  # precision defaults to 2
>>> print x
5.55
>>> x.set_precision(1)      # round to one fraction digit
>>> print x
5.6
>>> print FixedPoint("5.55", 1)  # same thing setting to 1 in constructor
5.6
>>> repr(x) #  returns constructor string that reproduces object exactly
"FixedPoint('5.6', 1)"
>>>

When FixedPoint objects of different precision are combined via + - * /,
the result is computed to the larger of the inputs' precisions, which also
becomes the precision of the resulting FixedPoint object.

>>> print FixedPoint("3.42") + FixedPoint("100.005", 3)
103.425
>>>

When a FixedPoint is combined with other numeric types (ints, floats,
strings representing a number) via + - * /, then similarly the computation
is carried out using-- and the result inherits --the FixedPoint's
precision.

>>> print FixedPoint(1) / 7
0.14
>>> print FixedPoint(1, 30) / 7
0.142857142857142857142857142857
>>>

Mirala, si te sirve decime.

Exitos,

Walter.


----- Original Message ----- 
From: "Javier Santana" <qualopec en gmail.com>
To: "La lista de python en castellano" <python-es en aditel.org>
Sent: Friday, April 28, 2006 4:31 PM
Subject: Re: [Python-es] flotantes


El problema de hacer eso es que puede que al multiplicar por 1000
provoques que el entero (o tipo que estés usando) se salga de rango.

On 4/28/06, Turriano <turriano en terra.es> wrote:
> Si alguien tiene una solución mas elegante, encantado de saberla, pero yo 
> me
> se un truco muy antiguo:
> Consiste en multiplicar el decimal por mil, eliminar los decimales y
> volverlo a dividir entre mil otra vez.
>
>
> ----- Original Message -----
> From: "Martha Lucía Gallego López" <mgallegol en gmail.com>
> To: <python-es en aditel.org>
> Sent: Friday, April 28, 2006 3:00 AM
> Subject: [Python-es] flotantes
>
>
> Hola un saludo a todos en la lista:
>
> Mi inquietud se da por el manejo en flotantes, como hago para que después 
> de
> hacer una operación con float en python por ejemplo que de la operación el
> resultado sea este 33.33854296 y lo único que yo quiero que muestre como
> resultado sea 33.338 es decir 3 decimales.
>
> Como podria hacer esto.
>
> Gracias,
>
> Un saludo,
>
> Martha L.
> _______________________________________________
> Python-es mailing list
> Python-es en aditel.org
> http://listas.aditel.org/listinfo/python-es
>
>
> _______________________________________________
> Python-es mailing list
> Python-es en aditel.org
> http://listas.aditel.org/listinfo/python-es
>
_______________________________________________
Python-es mailing list
Python-es en aditel.org
http://listas.aditel.org/listinfo/python-es


-- 
Internal Virus Database is out-of-date.
Checked by AVG Free Edition.
Version: 7.1.385 / Virus Database: 268.4.4/320 - Release Date: 20/04/2006




Más información sobre la lista de distribución Python-es