Click here to Skip to main content
13,299,495 members (67,816 online)
Click here to Skip to main content
Add your own
alternative version


12 bookmarked
Posted 6 Jul 2008

XSLT Number to String Transformation

Rate this:
Please Sign up or sign in to vote.
An XSLT transform for string representation of numeric place values.


We all have seen many applications written in different languages to transform numerical data to its equivalent place value representation. For example: 123 represented as 'One Hundred Twenty Three'. I am learning XSLT, and wrote this application to perform this transformation from XML to HTML numeric to place value translation. This transform file uses many features of XSLT like in-built methods for string manipulation, formatting, and number conversion.


People unfamiliar with India’s traditional numbering system may find this challenging. The system used in India, Pakistan, and Bangladesh is based on a unique grouping of two decimal places, rather than three decimal places common in the West. During business dealings in India, people are likely to come across the numerical terms Arab, Crore, and Lakh (see table below), although the higher numbers listed are rarely used. These more common words are often used in combination, e.g., one Lakh Crore, which is 1012, or one trillion. The terms Padma and Kharab are sometimes used in Hindi.

India’s unique number system

Term Figure

No of zeros

Western system (short-scale)

Lakh (lac) 1,00,000


100000 (100 thousand)

Crore 1,00,00,000


10,000,000 (Ten million)

Arab 1,00,00,00,000


1,000,000,000 (One billion)

Kharab 1,00,00,00,00,000


100,000,000,000 (100 billion)

Neel 1,00,00,00,00,00,000


10,000,000,000,000 (10 trillion)

Padma 1,00,00,00,00,00,00,000


1,000,000,000,000,000 (One quadrillion)

Shankh 1,00,00,00,00,00,00,00,000


100,000,000,000,000,000 (100 quadrillion)

Maha-shankh 1,00,00,00,00,00,00,00,00,000


10,000,000,000,000,000,000 (10 quintillion)

Using the code

Make your XML document as in the specified format given below. Put a stylesheet reference to your XML and open the XML in a web browser (in this XML, # represents a digit):

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="TransformNumber.xslt" ?>

Conversion algorithm

  1. Read each number in the XML and check if it contains a hundred place value. If yes, insert a '0' character at the fourth place in the string from the right position. (This step handles the exception since only a hundred place value is evaluated as a single digit in the Indian place-value number system; the rest of the places have paired values.)
  2. (1234 -> 10234)
  3. Check if the string has an odd number of characters after Step 1. If yes, prefix the string with a '0' character. (This step makes sure that the string contains an even number of characters; thus the string will be split into digit pairs later on and translated to its string equivalent.)
  4. (10234->010234)
  5. For each pair, call the translator with the pair value and its position in string. The position is determined by dividing the length of the string by 2.
  6. value:01 place:3 evaluates to 'One Thousand'
    value:02 place:2 evaluates to 'Two Hunderd'
    value:34 place:1 evaluates to 'Thirty Four'

    resulting in the string: 'One Thousand Two Hundred Thirty Four'.

Translator algorithm

Each pair contains two digits.

  1. Separate the first digit and postfix it with a '0' character. Translate it into its equivalent string representation. Don't do anything if the value is zero.
  2. (34 -> 3 + '0' -> 30 -> Thirty)
  3. Take the second digit and put its corresponding string representation. Don't do anything if the value is zero.
  4. (34 -> 4 -> Four)
  5. Take the place value and translate it to represent the actual place of this string pair in the whole number. Don't do anything if the place is zero.
  6. (3 -> 3-1 -> 2 -> Thousand)

Points of interest

The XSLT file in this project can help you understand calling recursive templates, and also gives an idea about how to use in-built functions. We can use this as a base and can design our own XSLT for the English number system translation.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mahendra Kumar Srivastava
Team Leader M*Modal
India India
Started carreer with CEERI, Delhi, India as a C Programmer in 2004 and migrated my learning experiance to C#.Net in 2005 with DIGISIGN Noida, Joined Bally in 2006, worked with JK Technosoft since Jun 2007 till Apr 2009, joined back Bally in June 2009 and joined Misys in June 2010. Worked in different domains such as Casino Management, Digital Signage, and Medical and currently working M*Modal as Lead - Product Development.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 7 Jul 2008
Article Copyright 2008 by Mahendra Kumar Srivastava
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid