villaacu.blogg.se

Numbers to text converter
Numbers to text converter






numbers to text converter

Number to convert: 2056 (baseNum= 1000 baseNumStr="thousand") Perhaps the best way to understand it is to see how a number progresses through each step, with the following example: The inline comments explain the logic of each step. This is clearly the trickiest part of the entire program. The final argument ( baseNumStr) specifies the textual version of that order of magnitude (e.g. The second argument ( baseNum) specifies how "big" the number is (whether it is in the 100s, 1000s, or 100000s, etc). Return bigPartStr + separator + ConvertNumberToString(restOfNumber) Step 3: concatenate 1st part of string with recursively generated remainder: int restOfNumber = n - bigPart * baseNum Step 2: check to see whether we're done: if (n % baseNum = 0) return bigPartStr String bigPartStr = ConvertNumberToString(bigPart) + " " + baseNumStr Step 1: strip off first portion, and convert it to string: int bigPart = ( int)(Math.Floor(( double)n / baseNum)) Strategy: translate the first portion of the number, then recursively translate the remaining sections. special case: use commas to separate portions of the number, unless we are in the hundreds string separator = (baseNumStr != " hundred") ? ", " : " "

numbers to text converter

"hundred", "thousand", or "million", etc) /// Textual representation of any integer private static string ConvertBigNumberToString( int n, int baseNum, string baseNumStr) / /// The numeric value of the integer to be translated ("textified") /// Represents the order of magnitude of the number (e.g., 100 or 1000 or 1e6, etc) /// The string representation of the base number (e.g. / /// This is the primary conversion method which can convert any integer bigger than 99 For this, I created a single method that takes three arguments: Things get slightly more interesting with the larger numbers. String onesStr = ConvertDigitToString(n - tensDigit * 10) Throw new IndexOutOfRangeException( String.Format( " not in range 20-99", n))

numbers to text converter

Private static string ConvertDigitToString( int i) The integer converter is trivial, being little more than basic boilerplate: If this had been a larger scale project, I would have placed them in the ViewModel module, but for such a minimalistic application, I didn't bother. The ConvertersĪdhering to the convention of MVVM to avoid using the code-behind file, I placed the converter code in a file of its own. I used the same convention to convert the integers into text. In order to do so, I added a custom window resource object to point to the converter which was defined in a separate file (in the GUI.Converters namespace). Now, due to the fact that the default value-type of the scrollbar is double, I needed to use a wpf-style converter to translate it into integer. Then, using basic databinding, I bound the first label to the value of the scrollbar, and a second label to the content of the first. First I added a simple horizontal scrollbar to use as the number-selecting control. The StackPanel consists of a few small controls. The xaml for this project is rather basic. When you build and run the code you will see this simple interface:Īs I mentioned above, the code is separated into a number of independent modules: 1. I separated all three modules into independent projects/assemblies to help preserve separation of concerns. Once the converter was complete, I added a graphical front end using WPF & xaml. I built the algorithm from the ground up, using TDD as my guide. I wanted to use this excercise as an opportunity to dust off my skills in TDD, WPF and algorithm design. I was pleased with the result and thought it would make a good article for my first submission to CodeProject. How hard would it be to "teach the computer" how to translate numbers (integers) into words? My ultimate goal was to create a simple WPF control with a scroll bar bound to a label that can range across all positive integers and a second label that would convert those numbers to their textual representation. On a recent snowy afternoon I decided to give myself a little coding challenge. Download NumberConverterToText.zip - 64.8 KB.








Numbers to text converter