Part of the series

Several example codes

~1 min read • Updated Oct 11, 2025

Program Overview

In many financial markets, stock prices are expressed as fractions—such as 1/8, 1/2, or 7/8.
This Python program reads two integers from the user: numerator and denominator of the fraction.
It then converts the fraction to a decimal (double) and displays the result as the numeric value of the stock.


Python Code:


def convert_fraction_to_double(numerator: int, denominator: int) -> float:
    if denominator == 0:
        raise ValueError("Denominator cannot be zero.")
    return numerator / denominator

# Run the program
numerator = int(input("Enter the numerator: "))
denominator = int(input("Enter the denominator: "))

try:
    value = convert_fraction_to_double(numerator, denominator)
    print(f"Decimal stock value: {value:.4f}")
except ValueError as e:
    print(f"Error: {e}")

Sample Output (input: 7 and 8):


Decimal stock value: 0.8750

Step-by-Step Explanation:

- The user enters the numerator and denominator of the stock fraction
- These values are passed to the convert_fraction_to_double method
- The method calculates the decimal value using division
- If the denominator is zero, an error message is shown
- The result is printed with four decimal places for precision


Written & researched by Dr. Shahin Siami