大数计算器(人类可读格式)

Win*_*nix 5 software-recommendation

我正在寻找适合大数字的计算器,其中可以输入一个字母而不是一大堆零:

  • K (Kilo) 000
  • M (Mega) 000,000 .............................. 百万
  • G (Giga) 000,000,000 ....................... 十亿
  • T (Tera) 000,000,000,000 ..................... 万亿
  • P (Peta) 000,000,000,000,000 ........ 万亿
  • E (Exa) 000,000,000,000,000,000. 万亿

例如,20 万亿美元/5000 万单位将输入为:

20t / 50m
Run Code Online (Sandbox Code Playgroud)

结果是 400,000,可以显示为400 K

是否已经有用于 Ubuntu / Debian 的大数字计算器(又名人类可读格式)?

我的问题类似于这些问题,但不重复:

所使用的字母如 T 代表 Tera (Trillion) 或 G 代表 Giga (Billion) 等,均来自行业标准。标准由电气和电子工程师协会(IEEE)制定 。


编辑:原始问题发布几天后,这变成了一个自我回答的问题。原始答案和替代建议是受欢迎的,并且可能会引起其他人的兴趣。

Win*_*nix 7

通过使用这个现成的Python 计算器 GUI,最初的解决方案只需要几分钟。

添加一点代码

完整的脚本如下,但总结一下在顶部附近插入这些行:

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')
Run Code Online (Sandbox Code Playgroud)

在底部附近插入这些行:

  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)
Run Code Online (Sandbox Code Playgroud)

在中间插入这些行:

 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]
Run Code Online (Sandbox Code Playgroud)

HDPI 显示器(1920x1080 像素)上的较大字体还有一些其他外观变化

示例计算器窗口

计算.py.png

  • 这是一个非常标准的计算器布局。
  • 注意我们添加的底行键。
  • 您可以单击键或简单地输入T而不是单击T按钮。
  • 还要注意我们如何输入20 t / 50 M代表20 万亿除以50 百万
  • 结果将显示为 400 K

完整的 Python 脚本

#-*-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()
Run Code Online (Sandbox Code Playgroud)

非常感谢作者(在上面的第一个链接上)贡献了这段代码!

安装 Tkinter

您需要安装python-tk( Tkinter ) 才能使用 Python GUI Calculator:

sudo apt update
sudo apt install python-tk
Run Code Online (Sandbox Code Playgroud)