Difference between revisions of "W1175 Numbers to Words"
Blake-kohl (talk | contribs) (Created Numbers to Words Page. Basic idea for the code is written.) |
(No difference)
|
Revision as of 11:31, 1 February 2021
Prerequisites[edit]
Introduction[edit]
Previously we built algorithms that converted a String to an Integer. Similarly, we can do the opposite and convert a number into any String that we want. For this exercise we will be taking an Integer and writing out in full the words that write out our number.
Writing out Numbers[edit]
Instead of just converting 25 to "25", in this exercise we are going to convert 25 to "Twenty Five".
1 One 12 Twelve 123 One Hundred Twenty Three 1234 One Thousand Two Hundred Thirty Four
Obtaining Individual Digits[edit]
To do this we need to deconstruct an Integer with multiple digits into individual digits. We did the opposite of in String to Integer, we took single digits and made it into a multi-digit number. Instead of multiplying digits by 10, we need to divide by 10. More specifically we need to get the remainder of each division, and that will be our digit.
1234 % 10 = 4
Now we have our "ones" digit. We can now try to get our "tens" digit, "hundreds", etc. To get these, remember back to Number Systems how we can get each place value using simple operations.
Once you have gotten a digit separated, you can then perform your conversion to make that digit into words to build a string.
Conversion Functions[edit]
Now that you have a digit, you need to convert it from an Integer to the String of Words.
A good way to do this is to make a function for each place value that can take a number and return that number written out in words. So a "ones" function may take 5 and return "five". A "tens" function may take 8 and return "eighty".
Building functions will help keep your code modular and create sections of code that do one thing, and one thing well.
Key Concepts[edit]
- Using Remainders to obtain the current "Ones" place value of an Integer
- Using Functions to create code that does a specific action only
Exercises[edit]
- M1175-10 Complete Merlin Mission Manager Mission M1175-10
- C101 Numbers to Words