Number Of The Day Challenge
I’ve been given a tiny, daily task that involves some math and the result of it is a number that should be written down in words.
Numbers In Engineering
In engineering, numbers are very important and there is no room for mistake. Above exercise is fairly easy and should teach you how to correctly write down numbers as words. You might be familiar with the rule that:
Numbers less than 10 should be written as words and numbers bigger than 9 should be written in numerals.
Some examples of this rule would be similar to these sentences:
The third stage in the process requires much stricter controls
Filtration can take up to 15 minutes for every 6 litres of fluid
Seven components make up the water management system
Number that have been measured or calculated, for example sensor readings, lengths of source material etc should always be numerals:
830 MHz
6 mg
77 K
4 cm x 4 cm
The Task In Python
I’ve completed the above task by hand to practice this important skill, but I also decided to practice another skill at the same time, which is programming – Python in this example. Writing simple function to to the calculations and then using #num2words library to get the desired answer.
# #!/usr/bin/env
# Number Of The Day
# SpawnTerror 2021
from num2words import num2words
def get_the_number(number: int) -> str:
number = number + 79
number += number
number = number * 4
number = number / 2
number -= 34
number = number * number
number = 0.75 * number
return str(number)
try:
x = int(input("Enter number of the day:"))
print(num2words(get_the_number(x)))
except ValueError:
print('Only numbers!')
Examples of the output with integer check:
Enter number of the day: a
Only numbers!
Enter number of the day:42
one hundred and fifty-one thousand, eight hundred and seventy-five
Enter number of the day:455
three million, three hundred and thirteen thousand, eight hundred and three