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.



Downloads

Comments

  • numbers to words

    Posted by WyattM on 07/22/2007 12:05am

    Is there anyway that I could get this code in vb.net?

    • Re: numbers to words

      Posted by Ajay Vijay on 07/22/2007 12:46am

      The 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.

      Reply
    Reply
  • Convert to words

    Posted by JohnW@Wessex on 02/14/2005 05:39am

    I'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?

    • Re: Convert to words

      Posted by Ajay Vijay on 02/14/2005 05:48am

      By 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".

      • convert numric value comma sepreted as per digit like above example

        Posted by NARESH on 08/27/2012 11:02pm

        Hi 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

        Reply
      Reply
    Reply
  • 3999

    Posted by Doctor Luz on 02/12/2005 06:06pm

    You 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.

    • Using AND

      Posted by neumeier on 04/11/2005 01:03pm

      To comment on the use of AND in the number representation:
      In 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.

      Reply
    • Thanks for the comments.

      Posted by Ajay Vijay on 02/14/2005 05:35am

      Great thank to both you. This article is
      intented to be starting point for anyone wishing 
      to use the algorithms, and he/she can extend it.
      
      Luz, From end-user perspective I meant, for 
      instance we need to rank out students in 
      School/College/University. There I do not see any 
      ranking (to be displayed) higher than 1000 (or 
      whatever). By the last line in my comment I 
      meant, that representations above 3999 roman 
      numerals is not consistent. Some uses 'm' the 
      number of times for each thousand 
      (MMMMMMMMMMMMCCCLVII, for 12357), some re-uses I 
      for ten-thousand and you also mentioned one more 
      sign. So, the representation would be in-
      consistent in itself. Finally, sorry, Roman 
      Numerals are not developed by me. ;)
      
      John, We also put 'and' in between hundred and 
      the rest two-digit value, and I ignored the fact 
      for simplicity. Thanks for the recommendation, 
      anyways.

      Reply
    • Number in words

      Posted by JohnW@Wessex on 02/14/2005 05:22am

      Just a minor point. In the UK we would normally say One Hundred *and* Twenty Three.

      Reply
    • Re: Re: 9999

      Posted by Doctor Luz on 02/14/2005 05:07am

      I 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.

      Reply
    • Re: 9999

      Posted by Ajay Vijay on 02/13/2005 07:24am

      That'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
    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Today's competitive marketplace requires the organization to frequently release and deploy applications at the pace of user demands, with reduced cost, risk, and increased quality. This book defines the basics of application release and deployment, and provides best practices for implementation with resources for a deeper dive. Inside you will find: The business and technical drivers behind automated application release and deployment. Evaluation guides for application release and deployment solutions. …

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds