Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the python for Java's BigDecimal?

In Java, when we program money it is recommended to use the class BigDecimal for money. Is there something similar in python? I would like something object-oriented that can have a currency and an exchange rate, has that been done?

I store money as integers of cents (I think) and multiply with 100 to get dollars but I also have foreign currency so listing listing ordered by price becomes inconvenient when articles have different currencies and even are listed as price per hour or per item. So ideally I would like a class for money in python that has exchange rate, currency and what type of pricing it is, if the pricing is per hour or per item.

So I suppose I'm looking for a priceclass and not a moneyclass, is there such a thing already? The gae I'm programming at doesn't have decimal data type so I can use an integer representing just the fraction or invent something like an own decimal representation where I implement it.

like image 923
Niklas Rosencrantz Avatar asked Jun 08 '13 05:06

Niklas Rosencrantz


People also ask

How do I declare BigDecimal?

BigDecimal round​(MathContext mc): This method returns a BigDecimal rounded according to the MathContext settings. int scale​(): This method returns the scale of this BigDecimal. BigDecimal scaleByPowerOfTen​(int n): This method returns a BigDecimal whose numerical value is equal to (this * 10n).

What is the package of BigDecimal?

A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale. If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale.

What is the default value for BigDecimal?

If you are using type BigDecimal, then its default value is null (it is object, not primitive type), so you get [1] automatically.


1 Answers

The java.math.BigDecimal in Java's equivalent is decimal.Decimal in Python. You can even go through the documentation for more. mpmath is a pure-Python library for multi-precision floating-point arithmetic.

like image 166
AllTooSir Avatar answered Oct 07 '22 08:10

AllTooSir