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.
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}")
Decimal stock value: 0.8750
- 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