Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from flask import Flask, request, render_template_string
|
| 2 |
from decimal import Decimal, ROUND_HALF_UP
|
| 3 |
import re
|
| 4 |
from datetime import datetime
|
|
@@ -79,25 +79,33 @@ def calculate_expression(expression):
|
|
| 79 |
# 最初の項目を取得
|
| 80 |
value1, unit1 = parse_value_unit(terms[0])
|
| 81 |
total_value = value1
|
|
|
|
| 82 |
decimal_places_list = [len(str(value1).replace('.', '').replace('-', ''))] # 有効数字のリストを保存
|
| 83 |
|
| 84 |
for i in range(1, len(terms)):
|
| 85 |
value2, unit2 = parse_value_unit(terms[i])
|
|
|
|
| 86 |
|
| 87 |
-
|
| 88 |
-
if unit1 != unit2:
|
| 89 |
value2 = convert_to_common_unit(value2, unit2, unit1)
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
log(f"Current total: {total_value}, current significant figures list: {decimal_places_list}")
|
| 94 |
|
| 95 |
# 最も少ない有効数字に基づいて丸める
|
| 96 |
min_sig_figs = min(decimal_places_list)
|
| 97 |
rounded_result = total_value.quantize(Decimal('1e{0}'.format(-(min_sig_figs - 1))), rounding=ROUND_HALF_UP)
|
| 98 |
-
log(f"Final result: {rounded_result} [{
|
| 99 |
|
| 100 |
-
return f"{rounded_result} [{
|
| 101 |
|
| 102 |
# HTMLテンプレート(バイト列でエンコード)
|
| 103 |
template = b"""
|
|
@@ -185,4 +193,4 @@ def calculate():
|
|
| 185 |
return f"Error: {str(e)}", 400
|
| 186 |
|
| 187 |
if __name__ == '__main__':
|
| 188 |
-
app.run(debug=True,host="0.0.0.0",port=7860)
|
|
|
|
| 1 |
+
from flask import Flask, request, render_template_string
|
| 2 |
from decimal import Decimal, ROUND_HALF_UP
|
| 3 |
import re
|
| 4 |
from datetime import datetime
|
|
|
|
| 79 |
# 最初の項目を取得
|
| 80 |
value1, unit1 = parse_value_unit(terms[0])
|
| 81 |
total_value = value1
|
| 82 |
+
total_unit = unit1
|
| 83 |
decimal_places_list = [len(str(value1).replace('.', '').replace('-', ''))] # 有効数字のリストを保存
|
| 84 |
|
| 85 |
for i in range(1, len(terms)):
|
| 86 |
value2, unit2 = parse_value_unit(terms[i])
|
| 87 |
+
decimal_places_list.append(len(str(value2).replace('.', '').replace('-', '')))
|
| 88 |
|
| 89 |
+
if unit1 != unit2 and ('*' not in expression and '/' not in expression):
|
|
|
|
| 90 |
value2 = convert_to_common_unit(value2, unit2, unit1)
|
| 91 |
|
| 92 |
+
if '*' in expression:
|
| 93 |
+
total_value *= value2
|
| 94 |
+
total_unit = f"{total_unit}*{unit2}"
|
| 95 |
+
elif '/' in expression:
|
| 96 |
+
total_value /= value2
|
| 97 |
+
total_unit = f"{total_unit}/{unit2}"
|
| 98 |
+
else:
|
| 99 |
+
total_value += value2
|
| 100 |
+
|
| 101 |
log(f"Current total: {total_value}, current significant figures list: {decimal_places_list}")
|
| 102 |
|
| 103 |
# 最も少ない有効数字に基づいて丸める
|
| 104 |
min_sig_figs = min(decimal_places_list)
|
| 105 |
rounded_result = total_value.quantize(Decimal('1e{0}'.format(-(min_sig_figs - 1))), rounding=ROUND_HALF_UP)
|
| 106 |
+
log(f"Final result: {rounded_result} [{total_unit}]")
|
| 107 |
|
| 108 |
+
return f"{rounded_result} [{total_unit}]"
|
| 109 |
|
| 110 |
# HTMLテンプレート(バイト列でエンコード)
|
| 111 |
template = b"""
|
|
|
|
| 193 |
return f"Error: {str(e)}", 400
|
| 194 |
|
| 195 |
if __name__ == '__main__':
|
| 196 |
+
app.run(debug=True, host="0.0.0.0", port=7860)
|