[value [, context]]) |
value can be an integer, string, tuple, or another Decimal
object. If no value is given, returns Decimal("0")
. If
value is a string, it should conform to the decimal numeric string
syntax:
sign ::= '+' | '-' digit ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' indicator ::= 'e' | 'E' digits ::= digit [digit]... decimal-part ::= digits '.' [digits] | ['.'] digits exponent-part ::= indicator [sign] digits infinity ::= 'Infinity' | 'Inf' nan ::= 'NaN' [digits] | 'sNaN' [digits] numeric-value ::= decimal-part [exponent-part] | infinity numeric-string ::= [sign] numeric-value | [sign] nan
If value is a tuple, it should have three components,
a sign (0 for positive or 1 for negative),
a tuple of digits, and an integer exponent. For example,
"Decimal((0, (1, 4, 1, 4), -3))" returns Decimal("1.414")
.
The context precision does not affect how many digits are stored. That is determined exclusively by the number of digits in value. For example, "Decimal("3.00000")" records all five zeroes even if the context precision is only three.
The purpose of the context argument is determining what to do if value is a malformed string. If the context traps InvalidOperation, an exception is raised; otherwise, the constructor returns a new Decimal with the value of NaN.
Once constructed, Decimal objects are immutable.
Decimal floating point objects share many properties with the other builtin numeric types such as float and int. All of the usual math operations and special methods apply. Likewise, decimal objects can be copied, pickled, printed, used as dictionary keys, used as set elements, compared, sorted, and coerced to another type (such as float or long).
In addition to the standard numeric properties, decimal floating point objects also have a number of specialized methods:
) |
Decimal("321e+5").adjusted()
returns seven. Used for determining the position of the most significant
digit with respect to the decimal point.
) |
other[, context]) |
a or b is a NaN ==> Decimal("NaN") a < b ==> Decimal("-1") a == b ==> Decimal("0") a > b ==> Decimal("1")
other[, context]) |
other[, context]) |
[context]) |
Decimal("32.100")
and
Decimal("0.321000e+2")
both normalize to the equivalent value
Decimal("32.1")
.
exp [, rounding[, context[, watchexp]]]) |
If watchexp is set (default), then an error is returned whenever the resulting exponent is greater than Emax or less than Etiny.
other[, context]) |
Decimal("-2")
which is closer to zero than Decimal("4")
.
If both are equally close, the one chosen will have the same sign as self.
other[, context]) |
[context]) |
[context]) |
Engineering notation has an exponent which is a multiple of 3, so there
are up to 3 digits left of the decimal place. For example, converts
Decimal('123E+1')
to Decimal("1.23E+3")
[rounding[, context]]) |
See About this document... for information on suggesting changes.