当前位置:网站首页>【案例】一个超级好用的工具 —— 给程序员用的计算器
【案例】一个超级好用的工具 —— 给程序员用的计算器
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
边栏推荐
- After clicking the uniapp e-commerce H5 embedded applet, the page prompts "the page iframe does not support referencing non business domain names"
- [turn] explain awk (1)__ Awk Basics_ Options_ Program segment parsing and examples
- JS convert text to language for playback
- Echart柱状图:堆叠柱状图value格式化显示
- Wechat applet (get location)
- 超有范的 logo 在线设计制作工具
- MySQL trigger
- 1+1 > 2, share creators can help you achieve
- Not in the following list of legal domain names, wechat applet solution
- A brief analysis of the overall process of view drawing
猜你喜欢

Wechat applet: basic review

Free screen recording software captura download and installation

JS to realize bidirectional data binding

Detailed explanation of Yanghui triangle

超有范的 logo 在线设计制作工具
![[to]12 common IP commands in the iproute installation package](/img/65/a214d137e230b1a1190feb03660f2c.jpg)
[to]12 common IP commands in the iproute installation package

The difference between the increment and decrement operators before and after variables i+, +i, I –, – I
![[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
![[turn] explain awk (2)_ Combining formatted output with built-in variables to realize requirements](/img/65/a214d137e230b1a1190feb03660f2c.jpg)
[turn] explain awk (2)_ Combining formatted output with built-in variables to realize requirements
![[solution] camunda deployment process should point to a running platform rest API](/img/ef/5b893e9c315c10db6c1db46b4c3f5a.jpg)
[solution] camunda deployment process should point to a running platform rest API
随机推荐
Security baseline check script - the road to dream
华为开发者认证与DevEco Studio编译器下载
【js】var、let、const
Echart histogram: echart implements stacked histogram
Uniapp dynamically shows / hides the navigation bar return button
Status management --provider
Fragment lifecycle
Wechat applet (get location)
欧姆龙平替国产大货—JY-V640半导体晶元盒读写器
Local file search tool everything
Essays on November 5, 2021
Wechat applet jumps to H5 page with parameters
Learning records countless questions (JS)
Sqlplus connection failure
Pod libwebp error reporting solution
Waterfall flow layout of uni app Homepage
MySQL stored procedure
Binary search
Introduction to USB learning (I) -- Dongfeng night flower tree
php 分布式事务 原理详解