Convert Numbers to Various Display Formats
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.
What Remains?
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.

Comments
numbers to words
Posted by WyattM on 07/22/2007 12:05am-
ReplyRe: numbers to words
Posted by Ajay Vijay on 07/22/2007 12:46amThe code presents the entire logic for conversion. Just adapt the C++ code into VB.NET. Here is idea: 1. Take one conversion-routine. 2. Copy and paste into VB.NET 3. Make necessary changes as per language 4. Compile, Run and Test. 5. If step 5 went thru, goto step 1 for next routine. See, the routines as coded in C++ language - but the routine is derived from some "Algorithm", which is NOT language dependent.
ReplyConvert to words
Posted by JohnW@Wessex on 02/14/2005 05:39amI've searched the Web for international standards on representing numbers as words. I found an ISO international time/date specification, but nothing on numbers. When you say international, are you sure you don't mean American?
-
ReplyRe: Convert to words
Posted by Ajay Vijay on 02/14/2005 05:48amBy International, I just mean the conversion-program which processes numbers in each 3-digits as once. That is there is group of 10 ^ (multiple of 3).
To clarify, this with Indian Format, we use Three digits for last (least significant) digits, and the use 2-2 groups. For example 12345678, would be:
1,23,45,678
and which would be written as:
One Crore Twenty Three Lakh Fourty Five Thousand Six Hundred and Seventy Eight (1 Crore 23 Lacs 45 Thousand 6 Hundred 78)
So, in my (and my country's) standpoint of view, the 3-digit convention is "International".
-
Replyconvert numric value comma sepreted as per digit like above example
Posted by NARESH on 08/27/2012 11:02pmHi I am Naresh How to convert numric value comma sepreted as per digit please tell me the format like above example. To clarify, this with Indian Format, we use Three digits for last (least significant) digits, and the use 2-2 groups. For example 12345678, would be: 1,23,45,678
Reply3999
Posted by Doctor Luz on 02/12/2005 06:06pmYou say "Due to the limited characters used in Roman numerals, the maximum value that can be converted is 3999" But this is incorrect. 3999 is not the maximum number you can write with the roman system. For example 4000 is written as follows: __ IV This is because roman number with a line abobe is multiplied by 1000. However is true that roman numbers bigger than 3999 are difficult to write, for example in an edit box.
-
-
-
-
-
ReplyUsing AND
Posted by neumeier on 04/11/2005 01:03pmTo comment on the use of AND in the number representation:
ReplyIn the English definition, AND should be used only in place of a decimal point, not in the number itself. It is probably the most abused (and excepted) for of writing words. 123.45 Should be written One Hundred Twenty Three and Forty-Five. One Hundred and Twenty Three is incorrect (but again, acceptable ;)
I remember getting in trouble in grade school for messing up this distinction.
Thanks for the comments.
Posted by Ajay Vijay on 02/14/2005 05:35amReply
Number in words
Posted by JohnW@Wessex on 02/14/2005 05:22amJust a minor point. In the UK we would normally say One Hundred *and* Twenty Three.
ReplyRe: Re: 9999
Posted by Doctor Luz on 02/14/2005 05:07amI don't see the reason why an "end-user" would not have a very large roman numerals conversion. And I don't want to discuss this. I would agree with you that 3999 might be a limit for practical programming purposes always thinking in the problem that represents displaying bigger numbers than 3999. With my previous comment, I only wanted to say that 3999 is not the real limit for roman numbers. As as you wrote your article, somebody reading it and not knowing anything about roman numbers could think that 3999 is the real limit. What you want to say with your last sentence? I don't understand. Anyway congratulations for your article and keep the good work.
ReplyRe: 9999
Posted by Ajay Vijay on 02/13/2005 07:24amThat's true. Limit is not 3999. But for practical programming purposes, this is limit. For instance from end-user's point of view this does not make sense to provide very large roman numerals converions. I have seen in Ms Word (Bullets and Numbering) that 3999, is not the maximum limit. But, as you know, presenting a series which is inconsistent in itself, does not offer much.
Reply