Building the Right Environment to Support AI, Machine Learning and Deep Learning
In this article, I present several algorithms to convert a given number in different display formats. Here, "display format" is from a user's point of view, not just converting values into alignment and rounding displays.
Although the code is written in VC++ and has used MFC's CString class exclusively, the algorithms in a general sense can be used in any language. Needless to say, each language and tool would require significant modification to the algorithms themselves apart from coding.
What It Does
Here is list of all functions provided in the sample, with brief descrpitions of all:
- CString ConvertToRoman(int nValue);
Converts the given integer value to a Roman numeral. For example, 1998 will convert to MCMXCVIII. Due to the limited characters used in Roman numerals, the maximum value that can be converted is 3999.
- CString ConvertToWords(UINT nValue);
Translates the given number to more a formal, human-readable format. If the input is 87,422,754, the result is Eighty Seven Million Four Hundred Twenty Two Thousand Seven Hundred Fifty Four. It can process values in the range of UINT, although can be easily extended to any 12-digit value. For higher values, you need to modify it a bit more.
- CString CommaFormat(double nValue,int nDecPlaces=0);
As the function prototype suggests, it converts the passed real number to a proper, comma-separated format. It also accepts numbers where precision is required, which defaults to zero. As an example, 12,600,565.76 would be the result for an input of 12600565.75904, with 2 digits of precision required. It does not check for any range and can also process negative values.
- CString ConvertToRank(int nValue);
Transforms the given number to 'rank.' A simple translatation what does is to convert the input value 43 to 43rd. It processes any positive integral value.
- CString ConvertToLetters(int nValue);
Converts the value to a less common display format. It does similar to column naming in spreadsheet packages. It can process in a range of 1 to 702, inclusive. Think about the reason behind this strange value. What it does is to convert 62 to BJ.
I hope that there would be problems with CommaFormat because it processes a double value. Also, ConvertToWords converts in International convention, which is not used in India. Indian convention is unique and is not used except in India (and perhaps its subcontinents). Any reader may request Indian conventional code from me.