Custom number formats
我有一些代码,目前设置为运行一组方程。我想允许输入自定义数字序列。而不是输入100,我想输入1 + 00,但我希望代码将其解释为100.运行方程后,我希望代码以X + XX格式返回结果。
我是编码的新手(这是我的第一段代码)并且已经四处寻找一种方法来格式化它。我知道在excel中我可以指定自定义数字格式"#+ ##。00",以便以我需要的格式输出一个单元格。我一直无法找到python等价物。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | from tkinter import * from math import * def show_entry_fields(): try: a, c, d, e, = float(e1.get()), float(e3.get()), float(e4.get()), float(e5.get()) b = e - d s = (a + b + c) / 2 height = (sqrt (s * (s - a) * (s - b) * (s - c)) * 2) / b height = float(format(height, '.3f')) height_label['text'] = str(height) side =((sqrt ((a ** 2) - (height ** 2))) + b) side = float(format(side, '.3f')) side_label['text'] = str(side) except ValueError: pass master.after(100, show_entry_fields) master = Tk() master.attributes("-topmost", True) master.title("Triangulation Plotting") Label(master, text="Measurement #1 Station Line Location").grid(row=1, column=0, sticky=W, pady=4) e4 = Entry(master) e4.grid(row=1, column=1, sticky=E) Label(master, text="Triangulation Measurement #1").grid(row=2, column=0, sticky=W, pady=4) e1 = Entry(master) e1.grid(row=2, column=1, sticky=E) Label(master, text="Measurement #2 Station Line Location").grid(row=3, column=0, sticky=W, pady=4) e5 = Entry(master) e5.grid(row=3, column=1, sticky=E) Label(master, text="Triangulation Measurement #2").grid(row=7, column=0, sticky=W, pady=4) e3 = Entry(master) e3.grid(row=7, column=1, sticky=E, pady=4) Label(master, text="Offset from station line").grid(row=8, column=0, sticky=W, pady=4) height_label = Label(master, text="") height_label.grid(row=8, column=1) Label(master, text="Measurement on Station Line").grid(row=9, column=0, sticky=W, pady=4) side_label = Label(master, text="") side_label.grid(row=9, column=1) master.after(100,show_entry_fields) master.mainloop() |
省略的部分是输入字段。数学和一切都完全按照需要出来。 X + XX的条目是用于特定应用程序的格式。我查看了docs下的str.format(),但没看到我怎么能这样做。
编辑:
因此,如果我输入2 + 66,它将被解释为266.它将根据其他输入进行计算,结果将以相同的格式显示。对于小于100或1 + 00的数字,它将输出前导0,即57 +的0 + 57.条目和结果将处于浮点状态。所有结果将加载到2位小数。
对于字符串,拆分
1 2 3 4 5 6 7 8 9 10 11 12 13 | def convert(thing): if isinstance(thing, str): a,b = thing.split('+') b,*d = b.split('.') d = '00' if not d else d[0] thing = round(float(f'{a}{b}.{d}'), 2) elif isinstance(thing, (int,float)): thing = str(round(thing, 2)) thing,*d = thing.split('.') d = '00' if not d else d[0] thing = thing if len(thing) > 2 else '0'+thing thing = f'{thing[:-2]}+{thing[-2:]}.{d}' return thing |
f-strings需要Python 3.6
对于Python 3.5-使用字符串格式:
1 2 3 | thing = round(float('{}{}.{}'.format(a,b,d)), 2) # and thing = '{}+{}.{}'.format(thing[:-2],thing[-2:],d) |
用法
数字字符串:
1 2 3 4 5 6 7 8 9 10 11 12 13 | >>> convert('2+66') 266.0 >>> convert('0+22') 22.0 >>> convert('2+66') + convert('0+22') 288.0 >>> x = '223+56.666' >>> convert(x) 22356.67 >>> y = '-54+22.33212' >>> convert(y) -5422.33 >>> |
数字到字符串
1 2 3 4 5 6 7 | >>> convert(22) '0+22.00' >>> convert(23454.22123) '234+54.22' >>> convert(-56732) '-567+32.00' >>> |