EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials XML Tutorial XSLT format-number
Secondary Sidebar
Python Newspaper

Magic Number in C

Phishing and Pharming

Shell Scripting Interview Questions

Software Testing Interview Questions

What is JavaScript?

XSLT format-number

XSLT format-number

Introduction to XSLT format-number

XSLT format-number is defined to convert a number into a string value using the second argument’s pattern. This is the type conversion formatting output. We use a different notation for numbers like comma for decimal formats and some points for thousands representation. To use all these, XSL uses format-number.

Syntax of XSLT format-number

The general syntax is given like this:

string format-number(value,format,[decimalformat])

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

The function carries three arguments:

  • value: formatted value.
  • format: string format.
  • name: an optional: name of xsl: decimal.

And their relevant attributes are given below:

The patterns symbols are defined as:

0 It specifies a digit used to specify the zero-digit decimal format (includes leading and trailing).
# Also, a digit with no leading and trailing zeros. A character used to represent a digit pattern.
. Used as decimal separator.
, Prefixed for grouping separator.
– Used as default negative prefix. Indicated by a minus sign.
; Used as a separator for positive and negative format patterns. The first pattern is for positive, and the second is for negative.
% It shows as a percentage character.
X Any characters can be added.
‘ Required for a quite specific.

How format-number Function Works in XSLT?

The format-number pattern says what to be printed, either leading zero or trailing zeros or the numbers with positive or negative.

For example, all countries never prefer to use the same characters to separate integer and decimal part, but this is accomplished using the format-number function. And at the same time, decimal-format statement should be declared at the top-level element in the code. As this identifier is passed as the third argument.

This is where we define the formatting like:

Code:

<xsl:variable name="size" select="./newval" />
<xsl:variable name="sizeAndSuffix">
<xsl:choose>
<xsl:when test="$size &gt;= 135247455">
<xsl:value-of select="format-number($size div 135247455,'###.###,##', 'educba')"/>
<xsl:text> TB</xsl:text>
</xsl:when>
<xsl:when test="$size &gt;= 135247">
<xsl:value-of select="format-number($size div 135247,'###.###,##', ' educba ')"/>
<xsl:text> GB</xsl:text>
</xsl:when>
<xsl:when test="$size &gt;= 1544">
<xsl:value-of select="format-number($size div 1544,'###.###,##', ' educba ')"/>
<xsl:text> Kilobyte</xsl:text>
</xsl:when>
<xsl:when test="$size &gt; 0 and $size &lt; 1024">
<xsl:value-of select="format-number($size div 0,'###.###,##', 'educba')"/>
<xsl:text> val</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>No Bytes</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>

Formatting numbers with commas.

For example, in a project, we had to display some field values in the following format:

18634449,4400
1,6000,00
63742585,300
We need to display them in the Euro.

XSLT format-number () is given as:

<xsl:value-of select='format-number(translate(@name,",","."), "#.##0,00", "european")'/>

With template:

<xsl:template match="/">
<xsl:variable name="docal" select="-23670102.5"/>
<xsl:value-of select="format-number($docal, '###,###.00',
'default-format')"/>
</xsl:template>
</xsl:stylesheet>
This gives the output as -23,670,102.5

The integer part has necessary digits, and the grouping separator with commas separates every three digits, and trailing zeros are not printed. Lastly, coming to the sign, the numbers are printed with positive and negative numbers, and the minus is prefixed with ‘- ‘.

Examples of XSLT format-number

Given below are the examples mentioned:

Example #1

The following code snippets show how to use decimal-number patterns and see how it functions in format-number.

XML File:

<?xml version="1.0"?>
<?xml-stylesheet href="format.xsl" type="text/xsl"?>
<Qty>-1.290</Qty>

XSL File:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:value-of select="format-number(Qty, '#0%')"/>
</xsl:template>
</xsl:stylesheet>

Explanation:

  • The example below shows how to format a negative value to the Qty element. And the above script produces the following output.

Output:

XSLT format-number 1

Example #2

XML File:

<?xm version="1.0" encoding="UTF-8"?>
<name> Rupees</name>

XSL File:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:decimal-format name="Rupees" decimal-separator="," grouping-separator="."/>
<xsl:template match="/">
<xsl:value-of select="format-number(56325.7, '#.###,00', 'Rupees')"/>
<br> </br>
<xsl:value-of select="format-number(8500.2, '#.00', 'Rupees')"/>
</xsl:template>
</xsl:stylesheet>

Explanation:

  • Declaring a ‘decimal format at the top of XSLT, and this effectively swaps over the separator.s.

Output:

XSLT format-number 2

Example #3

XML File:

<Values>
<leaf>200,011,000.01</leaf>
<leaf>210000000,02</leaf>
<leaf>110000000.03</leaf>
<leaf>360.000.000,04</leaf>
<leaf>301,000,000.05</leaf>
<leaf>200.000.000,06</leaf>
<leaf>106.000.110</leaf>
</Values>

XSL File:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Values">
<xsl:apply-templates select="node1]"/>
</xsl:template>
<xsl:template match="leaf">
<xsl:param name="sum" select="0"/>
<!-- grouping separator of this number -->
<xsl:variable name="vs">
<!-- the separators -->
<xsl:variable name="divider" select="translate(.,
'1234567890', '')"/>
<!-- take the second last character -->
<xsl:value-of select="substring($divider,
string-length($divider) - 1, 1)"/>
</xsl:variable>
<!-- the number of the current node -->
<!-- first remove the grouping separators, then replace ',' with
'.' -->
<xsl:variable name="num" select="translate(translate(., $vs, ''),
',', '.')"/>
<xsl:apply-templates select="following-sibling::node1]">
<xsl:with-param name="sum" select="$sum + $num"/>
</xsl:apply-templates>
<xsl:if test="not(following-sibling::node)">
<xsl:value-of select="$sum + $num"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

Explanation:

  • The above code adds the values together by supplying a grouping separator, and a node selects the respective fields.

Output:

adds the values together

Example #4

XML File:

<?xml version="1.0" encoding="UTF-16"?>
<LiveMarket>
<data>
<getNetSales>
<status>
<statusNo>On-Process</statusNo>
</status>
<Financial>
<ReportIncome>Marginrate5485</ReportIncome>
<mutualfunds>E3278</mutualfunds>
<Bucketmutualfunds>BBBB</Bucketmutualfunds>
<Stockindices>E3278 </Stockindices>
<qualityshare>-</qualityshare>
<qualityBucket>Std Quality</qualityBucket>
<Volume>77.0341</Volume>
<VolumeBucket>SC256.5645 and 5652.5652</VolumeBucket>
<VolumeGross>NO</VolumeGross>
<MarketCap>DC</MarketCap>
<MarketCapBucket>5:A04</MarketCapBucket>
<Previousclose>ETR</Previousclose>
<LastTrade>GBR-301850-T36S</LastTrade>
<Currency>52.076</Currency>
<Sharesoutstanding>10</Sharesoutstanding>
<SharesAdjustments>1.96</SharesAdjustments>
<opinionbuy>3.58969</opinionbuy>
<averagebuy>5.11151</averagebuy>
<marketlow>0.67883</marketlow>
<Total>3</Total>
<MaxPrice>1896.1628696544</MaxPrice>
<averagePrice>54265.858541</averagePrice>
<lowPrice>321474445.6325845</lowPrice>
</Financial>
</getNetSales>
</data>
</LiveMarket>

XSL Created File:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:msxml="urn:schemas-microsoft-com:xslt">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:decimal-format name="eu" decimal-separator=',' grouping-separator='.' />
<xsl:template match="*[not(*) and number()=number()]">
<xsl:copy>
<xsl:value-of select="format-number(., '#.##0,##########', 'eu')" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Explanation:

  • The above code formats all the decimal number in the XML document to European format. As the european format for decimal separator is “,” and for grouping it is “.”. So here, XSL changes the format at once.

Output:

XSLT format-number 4

Example #5

Showing a behavior of digit in a nice column used as a format character.

XML File:

<integernumber>
<integer>12</integer>
<integer>5.5</integer>
<integer>6.64</integer>
<integer>33.555</integer>
<integer>-7</integer>
<integer>4</integer>
<integer>334</integer>
<integer>2.1934</integer>
<integer>9.99</integer>
<integer>5.2351927</integer>
<integer>14</integer>
<integer>12</integer>
<integer>82</integer>
<integer>77</integer>
<integer>696</integer>
<integer>5715</integer>
<integer>-111111</integer>
<integer>24.66</integer>
<integer>18</integer>
<integer>34.54</integer>
<integer>63</integer>
<integer>99999</integer>
<integer>888888</integer>
<integer>88888888</integer>
<integer>52</integer>
<integer>45</integer>
<integer>-74.0001</integer>
</integernumber>

XSL File:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:variable name="vCols" select="3"/>
<xsl:template match="integernumbers">
<xsl:for-each select="integer[position( ) mod $vCols = 1]">
<xsl:apply-templates
select=". | following-sibling::integer[position( ) &lt; $vCols]"
mode="format"/>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
<xsl:template match="integer" name="format" mode="format">
<xsl:param name="integer" select="." />
<xsl:call-template name="leading-zero-to-space">
<xsl:with-param name="input"
select="format-number($integer,
'0000000.0000 ;0000000.0000- ')"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="leading-zero-to-space">
<xsl:param name="input"/>
<xsl:choose>
<xsl:when test="starts-with($input,'0')">
<xsl:value-of select="' '"/>
<xsl:call-template name="leading-zero-to-space">
<xsl:with-param name="input" select="substring-after($input,'0')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$input"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

Explanation:

  • The above code gives a very nice solution by displaying values in a column that uses a fixed-width format.

Output:

behavior of digit in a nice column

Advantages of XSLT format-number

Given below are the advantages of XSLT format-number:

  • The format-number() function is a great solution for a problem to display the number in various formats and has a rich set of attribute values.
  • It has the advantage of handling leading and trailing zero paddings and also a minus sign.
  • Formatting numbers helps in making a numeric character to display in arabic characters and roman-numeral format as well.
  • The tedious process of converting currency string is done simple.

Conclusion

In summary, the format-number() allows the user to configure the decimal number using a few patterns based on the number spacing. And we went through various examples, each with different cases.

Recommended Articles

This is a guide to XSLT format-number. Here we discuss the introduction, how the format-number function works in XSLT? examples and advantages. You may also have a look at the following articles to learn more –

  1. XML Schema
  2. XML Mapping
  3. XML ampersand
  4. XSLT substring
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more