当前位置:网站首页>【案例】一个超级好用的工具 —— 给程序员用的计算器
【案例】一个超级好用的工具 —— 给程序员用的计算器
2022-06-13 06:11:00 【Jia ming】
1. 前言
主要目的是解决不同类型数字(十进制、十六进程、二进制)相互计算时换算麻烦的问题,以及针对计算结果计算其十进制、十六进制、二进制大小和以可读形式展现其KB、MB、GB的大小。
特点:随意输入表达式、不同进制混用、输出多个进制转换结果、能够输出不同的存储单位值。
2. 源码展示
calculator.py
from ast import expr
k = 4
def alin_4(s):
# 每四位显示,高位补零
# 针对二进制和十六进制
r = []
s = '0'*(k - len(s) % k) + s if len(s) % k != 0 else s
# print("s", s)
for i in range(0, len(s) // k, 1):
# print(s[-k*(i+1):len(s)-k*i])
r.append(s[-k*(i+1):len(s)-k*i])
r.reverse()
return '_'.join(r)
while True:
result = int(eval(input()))
BIN = '0b_' + alin_4(bin(result)[2:])
HEX = '0x_' + alin_4(hex(result)[2:])
print(
"DEC:%.3f\nBIN:%s\nHEX:%s\nKB:%.3f\nMB:%.3f\nGB:%.3f\n" %
(result, BIN, HEX, result / 1024, result /1024 / 1024, result /1024 / 1024 / 1024)
)
3. 演示用例
Ctrl+C 结束输入。
PS E:\Python_prj> python -u "e:\Python_prj\calculator.py"
1 + 0x2 + 0b0011
DEC:6.000
BIN:0b_0110
HEX:0x_0006
KB:0.006
MB:0.000
GB:0.000
2 + 4 + 8 + 16 + 32 + 64
DEC:126.000
BIN:0b_0111_1110
HEX:0x_007e
KB:0.123
MB:0.000
GB:0.000
0xffff + 987654 + 0xFFFFFFFFFFFFFFFFFF
DEC:4722366482869646262272.000
BIN:0b_0001_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0001_0000_0001_0010_0000_0100
HEX:0x_0100_0000_0000_0010_1204
KB:4611686018427388928.000
MB:4503599627370497.000
GB:4398046511104.001
边栏推荐
- Echart histogram: stacked histogram displays value
- Echart histogram: X-axis displays value, Y-axis displays category
- js将文本转成语言播放
- Record the basic use of zxing, the Google open source library, to generate and read QR codes
- Wechat applet: use of global state variables
- Wechat applet development (requesting background data and encapsulating request function)
- php 分布式事务 原理详解
- Wechat applet (pull-down refresh data) novice to
- 安全基线检查脚本—— 筑梦之路
- [one · data 𞓜 simple implementation of the leading two-way circular linked list]
猜你喜欢

Echart histogram: stacked histogram displays value

Solution: vscode open file will always overwrite the last opened label

微信小程序:点击事件获取当前设备信息(基础)

Download and installation of universal player potplayer, live stream m3u8 import

Annotation only integration SSM framework

Echart柱状图:堆叠柱状图显示value

MySQL stored procedure
![[spark]spark introductory practical series_ 8_ Spark_ Mllib (upper)__ Introduction to machine learning and sparkmllib](/img/39/501b9b8962b03cfc59e2636f88ee93.jpg)
[spark]spark introductory practical series_ 8_ Spark_ Mllib (upper)__ Introduction to machine learning and sparkmllib

Binary search

Echart line chart: multiple line charts show only one line at a time
随机推荐
[JS] array de duplication
Applet Use of spaces
《MATLAB 神经网络43个案例分析》:第11章 连续Hopfield神经网络的优化——旅行商问题优化计算
The title of the WebView page will be displayed in the top navigation bar of the app. How to customize
php 分布式事务 原理详解
Echart柱状图:echart实现堆叠柱状图
微信小程序:点击事件获取当前设备信息(基础)
[turn] explain awk (2)_ Combining formatted output with built-in variables to realize requirements
Time complexity and space complexity
[DP 01 backpack]
Custom view - extensible collapsexpendview
Self summarizing
Basic knowledge of knowledge map
js将文本转成语言播放
USB status error and its cause (error code)
Echart折线图:当多条折线图的name一样时也显示不同的颜色
Fichier local second Search Tool everything
【var const let区别】
[spark]spark introductory practical series_ 8_ Spark_ Mllib (lower)__ Machine learning library sparkmllib practice
Solution: vscode open file will always overwrite the last opened label