VextoR
VextoR

Reputation: 5165

xslt 2.0 how to round number? 1.45632 => 1.46

How to round number in xslt 2.0

So for example it will work like this:

1.4367 => 1.44
1.3218 => 1.32

Upvotes: 4

Views: 5180

Answers (3)

cipherwizard
cipherwizard

Reputation: 21

You may use format-number(). A possible way to use is as below:

<xsl:value-of select="format-number(1.45632,'#.00')"/>

Note: if you don't want 00 to be there, in case no decimal digit is present then use '#.##' instead.

Upvotes: 1

Rahul Tripathi
Rahul Tripathi

Reputation: 172378

You are probably looking for format-number() function.

Also in In XSLT 2.0/XPath 2.0 one can use the xs:decimal type to work without loss of precision.

Something like this:

<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                exclude-result-prefixes="xs">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/*">
    <MyTable>
      <xsl:value-of select="xs:decimal(MyValue)"/>
    </MyTable>
  </xsl:template>
</xsl:stylesheet>

Upvotes: 1

Choppin Broccoli
Choppin Broccoli

Reputation: 3066

Try using the round function:

<xsl:value-of select="round(NUMBER_TO_ROUND*100) div 100"/>

Upvotes: 3

Related Questions