Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import math | |
| # === Utility Functions === | |
| def convert_number(input_str): | |
| try: | |
| if '× 10^' in input_str: | |
| base_str, exponent_str = input_str.split('× 10^') | |
| base = float(base_str.strip()) | |
| exponent = int(exponent_str.strip()) | |
| result = base * (10 ** exponent) | |
| return str(result) | |
| elif 'e' in input_str.lower(): | |
| result = float(input_str) | |
| return str(result) | |
| else: | |
| num = float(input_str) | |
| if num == 0: | |
| return "0 × 10^0" | |
| exponent = int(math.floor(math.log10(abs(num)))) | |
| base = num / (10 ** exponent) | |
| base = round(base, 6) | |
| return f"{base} × 10^{exponent}" | |
| except Exception as e: | |
| return f"Error: {e}" | |
| def parse_standard_form(s): | |
| try: | |
| base, exponent = s.split("× 10^") | |
| base = float(base.strip()) | |
| exponent = int(exponent.strip()) | |
| return base, exponent | |
| except: | |
| return None, None | |
| def standard_form_calculator(num1_str, num2_str, operation): | |
| a, m = parse_standard_form(num1_str) | |
| b, n = parse_standard_form(num2_str) | |
| if a is None or b is None: | |
| return "Invalid input format. Use format like: 3.2 × 10^4" | |
| if operation == "multiply": | |
| base = a * b | |
| exponent = m + n | |
| elif operation == "divide": | |
| base = a / b | |
| exponent = m - n | |
| else: | |
| return "Invalid operation" | |
| while base >= 10: | |
| base /= 10 | |
| exponent += 1 | |
| while base < 1: | |
| base *= 10 | |
| exponent -= 1 | |
| return f"{round(base, 6)} × 10^{exponent}" | |
| # === Gradio Interface === | |
| # Converter UI | |
| with gr.Blocks() as convert_ui: | |
| gr.Markdown("## 🔁 Standard ↔ Ordinary Conversion") | |
| input_box = gr.Textbox(label="Enter number (e.g., '2.3 × 10^4', '1e-3', or '23000')") | |
| output_box = gr.Textbox(label="Result", interactive=False) | |
| convert_btn = gr.Button("Convert") | |
| convert_btn.click(fn=convert_number, inputs=input_box, outputs=output_box) | |
| # Calculator UI | |
| with gr.Blocks() as calculator_ui: | |
| gr.Markdown("## ✖️➗ Standard Form Calculator") | |
| num1_input = gr.Textbox(label="First Number (e.g., '3.2 × 10^4')", value="3.2 × 10^4") | |
| num2_input = gr.Textbox(label="Second Number (e.g., '2 × 10^3')", value="2 × 10^3") | |
| operation_input = gr.Radio(["multiply", "divide"], label="Choose Operation") | |
| calc_result = gr.Textbox(label="Result", interactive=False) | |
| calc_btn = gr.Button("Calculate") | |
| calc_btn.click(fn=standard_form_calculator, inputs=[num1_input, num2_input, operation_input], outputs=calc_result) | |
| # Main app with Tab selection | |
| with gr.Blocks() as app: | |
| gr.Markdown("## 🔢 Standard Form Helper Tool By Rumaisa - Source Code Academia") | |
| tabs = gr.Tabs() | |
| with tabs: | |
| with gr.TabItem("Convert"): | |
| convert_ui.render() | |
| with gr.TabItem("Calculator"): | |
| calculator_ui.render() | |
| # Launch the app | |
| app.launch() | |