Big Number Calculator (human readable format)
I'm looking for a calculator suitable for large numbers where a letter can be entered instead of a whole bunch of zeros:
K (Kilo) 000
-
M (Mega) 000,000
.............................. Million -
G (Giga) 000,000,000
....................... Billion -
T (Tera) 000,000,000,000
............... Trillion -
P (Peta) 000,000,000,000,000
........ Quadtrillion -
E (Exa) 000,000,000,000,000,000
. Quintrillion
For example $20 trillion dollars / 50 million units would be entered as:
20t / 50m
The result is 400,000 and can be displayed as 400 K
.
Is there already a calculator for Ubuntu / Debian for Big Numbers (aka Human Readable format)?
My question is similar to these questions but not a duplicate:
- Add thousand separator to gnome calculator in Ubuntu 18.04
- How to show comma separation in numbers on Ubuntu's calculator?
The letters used such as T for Tera (Trillion) or G for Giga (Billion), etc. come from industry standards. Standards are set by Institute of Electrical and Electronics Engineers (IEEE).
Edit: A few days after original question was posted, this turned into a self-answered question. Original answers though and alternative suggestions are welcome and would likely interest others.
An initial solution only took a few minutes by taking this readily available Python Calculator GUI.
Add a little code
The full script is below but to summarize insert these lines near the top:
self.newtext=self.newtext.replace(' ','')
self.newtext=self.newtext.upper()
self.newtext=self.newtext.replace('K','000')
self.newtext=self.newtext.replace('M','000000')
self.newtext=self.newtext.replace('G','000000000')
self.newtext=self.newtext.replace('T','000000000000')
self.newtext=self.newtext.replace('P','000000000000000')
self.newtext=self.newtext.replace('E','000000000000000000')
Insert these lines near the bottom:
Button(master,text="E",width=3,command=lambda:self.action('E')).grid(row=5, column=0)
Button(master,text="P",width=3,command=lambda:self.action('P')).grid(row=5, column=1)
Button(master,text="T",width=3,command=lambda:self.action('T')).grid(row=5, column=2)
Button(master,text="G",width=3,command=lambda:self.action('G')).grid(row=5, column=3)
Button(master,text="M",width=3,command=lambda:self.action('M')).grid(row=5, column=4)
Button(master,text="K",width=3,command=lambda:self.action('K')).grid(row=5, column=5)
Insert these lines in the middle:
def convert(self):
#2**10 = 1024
power = 1000
size=self.value
n = 0
Dic_powerN = {0: '', 1: 'K', 2: 'M', 3: 'G', 4: 'T', 5: 'P', 6: 'E'}
while size > power:
size /= power
n += 1
return size, Dic_powerN[n]
There are a few other cosmetic changes for larger font on HDPI monitor (1920x1080 pixels)
Sample Calculator Window
- This is a pretty standard calculator layout.
- Notice the bottom row of keys we added.
- You can click the keys or simply type in
T
instead of clickingT
button. - Also notice how we entered
20 t / 50 M
to represent 20 Trillion divided by 50 Million. - The result will be displayed as
400 K
Full Python Script
#-*-coding: utf-8-*-
# NAME: calc.py
# CALL: python calc.py
# DATE: December 8, 2018
# DESC: Calculator in E-Exa, P-Peta, T-Tetra, G-Giga, M-Mega and K-Kilo
# NOTE: Requires Tkinter GUI libraries: sudo apt install python-tk
# Majority Credit to: https://www.techinfected.net/2016/02/make-gui-calculator-in-python-windows-linux.html
from Tkinter import *
import tkFont
import math
class calc:
def getandreplace(self):
"""replace x with * and ÷ with /"""
self.expression = self.e.get()
self.newtext=self.expression.replace(self.newdiv,'/')
self.newtext=self.newtext.replace('x','*')
self.newtext=self.newtext.replace(' ','')
self.newtext=self.newtext.upper()
self.newtext=self.newtext.replace('K','000')
self.newtext=self.newtext.replace('M','000000')
self.newtext=self.newtext.replace('G','000000000')
self.newtext=self.newtext.replace('T','000000000000')
self.newtext=self.newtext.replace('P','000000000000000')
self.newtext=self.newtext.replace('E','000000000000000000')
def equals(self):
"""when the equal button is pressed"""
self.getandreplace()
try:
self.value= eval(self.newtext) #evaluate the expression using the eval function
except SyntaxError or NameErrror:
self.e.delete(0,END)
self.e.insert(0,'Invalid Input!')
else:
self.e.delete(0,END)
self.value= self.convert() # Give result in K, M, G, T, P or E
self.e.insert(0,self.value)
def convert(self):
#2**10 = 1024
power = 1000
size=self.value
n = 0
Dic_powerN = {0: '', 1: 'K', 2: 'M', 3: 'G', 4: 'T', 5: 'P', 6: 'E'}
while size > power:
size /= power
n += 1
return size, Dic_powerN[n]
def squareroot(self):
"""squareroot method"""
self.getandreplace()
try:
self.value= eval(self.newtext) #evaluate the expression using the eval function
except SyntaxError or NameErrror:
self.e.delete(0,END)
self.e.insert(0,'Invalid Input!')
else:
self.sqrtval=math.sqrt(self.value)
self.e.delete(0,END)
self.e.insert(0,self.sqrtval)
def square(self):
"""square method"""
self.getandreplace()
try:
self.value= eval(self.newtext) #evaluate the expression using the eval function
except SyntaxError or NameErrror:
self.e.delete(0,END)
self.e.insert(0,'Invalid Input!')
else:
self.sqval=math.pow(self.value,2)
self.e.delete(0,END)
self.e.insert(0,self.sqval)
def clearall(self):
"""when clear button is pressed,clears the text input area"""
self.e.delete(0,END)
def clear1(self):
self.txt=self.e.get()[:-1]
self.e.delete(0,END)
self.e.insert(0,self.txt)
def action(self,argi):
"""pressed button's value is inserted into the end of the text area"""
self.e.insert(END,argi)
def __init__(self,master):
"""Constructor method"""
master.title('Calculator')
master.geometry()
font = "Calibri 13"
self.e = Entry(master, font = "Calibri 13")
# self.e = Entry(master)
self.e.grid(row=0,column=0,columnspan=6,pady=3)
self.e.focus_set() #Sets focus on the input text area
self.div='÷'
self.newdiv=self.div.decode('utf-8')
#Generating Buttons
# Button(master,text="=",width=10,command=lambda:self.equals()).grid(row=4, column=4,columnspan=2)
Button(master,text="=",width=8,command=lambda:self.equals()).grid(row=4, column=4,columnspan=2)
Button(master,text='AC',width=3,command=lambda:self.clearall()).grid(row=1, column=4)
Button(master,text='C',width=3,command=lambda:self.clear1()).grid(row=1, column=5)
Button(master,text="+",width=3,command=lambda:self.action('+')).grid(row=4, column=3)
Button(master,text="x",width=3,command=lambda:self.action('x')).grid(row=2, column=3)
Button(master,text="-",width=3,command=lambda:self.action('-')).grid(row=3, column=3)
Button(master,text="÷",width=3,command=lambda:self.action(self.newdiv)).grid(row=1, column=3)
Button(master,text="%",width=3,command=lambda:self.action('%')).grid(row=4, column=2)
Button(master,text="7",width=3,command=lambda:self.action('7')).grid(row=1, column=0)
Button(master,text="8",width=3,command=lambda:self.action(8)).grid(row=1, column=1)
Button(master,text="9",width=3,command=lambda:self.action(9)).grid(row=1, column=2)
Button(master,text="4",width=3,command=lambda:self.action(4)).grid(row=2, column=0)
Button(master,text="5",width=3,command=lambda:self.action(5)).grid(row=2, column=1)
Button(master,text="6",width=3,command=lambda:self.action(6)).grid(row=2, column=2)
Button(master,text="1",width=3,command=lambda:self.action(1)).grid(row=3, column=0)
Button(master,text="2",width=3,command=lambda:self.action(2)).grid(row=3, column=1)
Button(master,text="3",width=3,command=lambda:self.action(3)).grid(row=3, column=2)
Button(master,text="0",width=3,command=lambda:self.action(0)).grid(row=4, column=0)
Button(master,text=".",width=3,command=lambda:self.action('.')).grid(row=4, column=1)
Button(master,text="(",width=3,command=lambda:self.action('(')).grid(row=2, column=4)
Button(master,text=")",width=3,command=lambda:self.action(')')).grid(row=2, column=5)
Button(master,text="√",width=3,command=lambda:self.squareroot()).grid(row=3, column=4)
Button(master,text="x²",width=3,command=lambda:self.square()).grid(row=3, column=5)
Button(master,text="E",width=3,command=lambda:self.action('E')).grid(row=5, column=0)
Button(master,text="P",width=3,command=lambda:self.action('P')).grid(row=5, column=1)
Button(master,text="T",width=3,command=lambda:self.action('T')).grid(row=5, column=2)
Button(master,text="G",width=3,command=lambda:self.action('G')).grid(row=5, column=3)
Button(master,text="M",width=3,command=lambda:self.action('M')).grid(row=5, column=4)
Button(master,text="K",width=3,command=lambda:self.action('K')).grid(row=5, column=5)
#Main
root = Tk()
# Larger font for HDPI screen
default_font = tkFont.nametofont("TkDefaultFont")
default_font.configure(size=11)
obj=calc(root) #object instantiated
root.mainloop()
Many thanks to the author (on first link above) for contributing this code!
Install Tkinter
You need python-tk
(Tkinter) installed to use Python GUI Calculator:
sudo apt update
sudo apt install python-tk
For example $20 trillion dollars / 50 million taxpayers would be entered as:
20t / 50m
I will do a frame challenge
The answer here isn't to find software that supports this format, but to learn a standard format used in both calculators, programming languages and math: scientific notation.
You write a number as a base (significand) and an exponent, e.g. 2*10⁵ for 20000. 20 trillions is thus 2*10¹³ - or 20*10¹². This will work on nearly all calculators, allows for compact and exact numbers, independently of language. Billion may for instance mean both 10⁹ and 10¹², depending on culture.
In addition, this allows you to clearly define significant figures, as you may write 7.0*10³ or 7.02*10³, to note the number of known decimals.
In computers this may additionally be written as ney for n*10^y, for instance 5e6 is interpreted as 5*10⁶. This form can be used in for instance octave
.