# Coding Python Into A Ultimate Calculator

*23*

# Summary:

With only a few lines of code, you can use Python to calculate whatever it is you need for Python to calculate.

For example, if you're working with big numbers or complex numbers.

Another example is when you working with cryptocurrencies and you want to figure out what the numbers look like when you specify how many decimal places there are.

Lastly, you can customize the output towards whatever you want as along as the module we're going to using supports it.

*There will be 10 examples* provided for each scenario, so that it makes more sense to the reader of this article.

# Dependency:

This will require the **decimal** module to be installed.

# How To Understand This Article:

You understand this article by observing what's shown what code we type in that produces the following result. You learn a lot more, a lot quicker.

# Step #1:

Open up a Python Interpreter, for me personally I use Linux Mint, so I'll open up a Terminal window and type in.

`python2`

OR

`python3`

# Step #2:

Okay, now our Python Interpreter is active. Let's import everything from the **decimal** module as shown below by typing in...

`from decimal import *`

# Step #3:

You can view the current settings of the decimal module by typing in...

`getcontext()`

and press enter.

# Step #4:

If you need to change something for instance. Let's say that you want to the round to the nearest when the number next to the number that's being rounded is 5 and above. Then do this. Type in...

`getcontext().rounding = ROUND_HALF_UP`

and press enter. Type in...

`getcontext()`

and you can see that it used to display, "ROUND_HALF_EVEN". Now it displays "ROUND_HALF_UP".

Now that you know how to configure the settings. Let's move on shall we.

# Step #5:

Here we are. We are now going to get into the examples of how to use Python as a ultimate calculator. Let's start off with Example #1 below.

# Example #1:

You want to calculate what 2 ^ 256 in a **fixed format** with **thousand separators**.

First thing we need do is to remember to put the numbers within each decimal function, like so.

For 2 it would be...

`Decimal('2')`

For 256 it would be...

`Decimal('256')`

Then we type out the equation as shown below.

`Decimal('2') ** Decimal('256')`

** means ^ in python programming.

Don't forget to change the precision in order for the format function to print correctly depending on how big the number is. We can change the precision by typing in... below and then press enter. *NOTE: The precision can be specified as big as you want it to be, from 0 to 1,000 to 1,000,000... etc.*

`getcontext().prec = 1000`

This will have the format function print the number in a fixed format as long as it fits within the 1,000 digit workspace.

A **digit** can be a **whole number** or a **decimal number**.

Now we need to wrap the equation within **parenthesis** within the **format** function and add in the comma within the single quotes in order for the output to have thousand separators. As shown below...

`format( ( Decimal('2') ** Decimal('256') ), ',' )`

We are still not done yet. The only thing left to do is the wrap the format function within the print function. We will do that by typing in what's shown below and press enter.

`print( format( ( Decimal('2') ** Decimal('256') ), ',' ) )`

This will output the following result.

`115,792,089,237,316,195,423,570,985,008,687,907,853,269,984,665,640,564,039,457,584,007,913,129,639,936`

This number is only 78 digits. So the format function was able to print the number in a fixed format because of the fact that it fits within the 1,000 digit workspace.

You can also print the number without the thousand separators by removing the comma within the single quotes within the format function as shown below.

`print( format( ( Decimal('2') ** Decimal('256') ), '' ) )`

Result...

`115792089237316195423570985008687907853269984665640564039457584007913129639936`

# Example #2:

We want to print out 1 followed by 18 decimal places along with the Ether Ticker Symbol ( ETH ). We'll do that by typing in what's shown below. Making sure that the number fits within the precision that you have specified to begin with.

```
getcontext().prec = 1000 # If the precision is already specified toward your liking, you don't have to type it in again.
print( format( ( Decimal('1') ), '.18f' ) + ' ETH' )
```

Result...

`1.000000000000000000 ETH`

# Example #3:

You want Python to show you how much Bitcoin Cash ( BCH ) you'll receive if the price of BCH is set at 250.39 USD and you want to buy 50.00 USD worth of BCH.

Type in the code below and press enter.

`print( format( ( Decimal('50') / Decimal('250.39') ), '.8f' ) + ' BCH' )`

Result:

`0.19968849 BCH`

If you want to add in the thousand separators, type in what's shown below and press enter.

`print( format( ( Decimal('50') / Decimal('250.39') ), ',.8f' ) + ' BCH' )`

*NOTE: Within the **decimal** function, the number 50 will be the same as 50.00, it would not have an effect on the output if either one is typed in.*

# Example #4:

What if I want print out the maximum supply of Bitcoin that shows the 8 decimal places as well as the thousand separators from just typing in 20999999.976 within the decimal function.

Type in the code below and press enter.

`print( format( ( Decimal('20999999.976') ), ',.8f' ) )`

Result:

`20,999,999.97600000`

What if I want to add in it's Ticker Symbol? Type in and then press enter.

`print( format( ( Decimal('20999999.976') ), ',.8f' ) + ' BTC' )`

Result:

`20,999,999.97600000 BTC`

What if I don't want the Thousand Separators? Type in and then press enter.

`print( format( ( Decimal('20999999.976') ), '.8f' ) + ' BTC' )`

Result:

`20999999.97600000 BTC`

# Example #5:

If you want to add 4.9 DGB to 5.1 DGB, to print the number without it's Ticker Symbol and the trailing decimal places.

Type in...

`print( format( ( Decimal('4.9') + Decimal('5.1') ), '.0f' ) )`

Result...

`10`

# Example #6:

You want the output to be displayed as shown in GTA: San Andreas. You know with the leading zeros with this $ sign in front of it and stuff. You got it. Just type in what's shown below and press enter.

`print( '$' + format( ( Decimal('0') ), '08.0f' ) )`

Result:

`$00000000`

What if I want to add $350 to the balance? Type in...

`print( '$' + format( ( Decimal('350') + Decimal('0') ), '08.0f' ) )`

Result...

`$00000350`

What if subtract -$299 to the equation? Type in...

`print( '$' + format( ( Decimal('350') + Decimal('0') - Decimal('299') ), '08.0f' ) )`

Result...

`$00000051`

Also you can put in the **negative **or **subtract** sign within the **decimal** function just to be aware of that.

# Example #7:

What if I want the output to have a space in front of it? Type in and then press enter.

`print( ' ' + format( ( Decimal('0') ), '' ) )`

Result...

` 0`

# Example #8:

Can you do multiplication for instance? Can you multiply 5 × 9? Yes, type in...

`print( format( ( Decimal('5') * Decimal('9') ), '' ) )`

Result...

`45`

What about division? Can you do 5 / 9 and show only 5 decimal places? Yes, type in.

`print( format( ( Decimal('5') / Decimal('9') ), '.5f' ) )`

Information regarding to the rounding operating within the settings of getcontext().

**ROUND_HALF_EVEN** will produce the following effect. Look at the number in the 5 decimal place. If we specified the output to have only 4 decimal places.

`0.00005`

Result...

`0.0000`

Again...

`0.00006`

Result...

`0.0001`

**ROUND_HALF_UP**

`0.00005`

Result...

`0.0001`

Again...

`0.00004`

Result...

`0.0000`

**ROUND_DOWN**

`0.00009`

Result...

`0.0000`

Again...

`0.00001`

Result...

`0.0000`

**ROUND_UP**

`0.00009`

Result...

`0.0001`

Again...

`0.00001`

Result...

`0.0001`

# Example #9:

What if I want to figure out how much in μBCH would I receive, if I bought 100 USD worth of BCH when BCH price was set to 1,293.12 USD. Type in what's shown below and then press enter.

To type in the **μ** symbol, press Ctrl + Shift + U, then type in **03bc** and press enter.

For me this key combination to type in special characters works on Linux Mint. I don't know about Windows or macOS.

`print( format( ( ( Decimal('100') / Decimal('1293.12') ) * Decimal('1000000') ), ',.2f' ) + ' μBCH' )`

Result...

`77,332.34 μBCH`

# Example #10:

Is there a way, so that the output will be striped of any trailing zero there is and the decimal point if the conditions are met? Yes, create a file and put in the following code as shown below. Observe the effect of this code and it will start to make more sense. Here's a little bit more advance example.

```
# Import the module
from decimal import *
# Tweak the settings
getcontext().prec = 32
getcontext().rounding = ROUND_HALF_UP
# Declare Variables
a = ( Decimal('50') / Decimal('2102.31') )
b = format( a, ',.12f' )
c = '.000000000000'
# If Else Statement
if( search( c, b ) ):
print('')
print( b.replace( c, '' ) + ' XMR' )
print('')
else:
print('')
print( b.rstrip('0') + ' XMR' )
print('')
```

# Conclusion:

Thank you to all of those who sat down to read this article. I've been working on this since, the very beginning non-stop. Hopefully this helps you in your time of need of wanting to calculate some huge number or complex number. I know that these examples above didn't show any complex numbers. I get that, since, there's so much you can do with this tool. It is hard to put it all in one article. For more information about decimal arithmetic go to https://docs.python.org/3/library/decimal.html. Also, look in Python's document **Library Reference**, if you want to know how to calculate complex numbers. Anyway, I hope you appreciate my article I put out for today.