Tuesday, May 31, 2011

Round /Turncate numeric values in xslt

To round a numeric value to some specific decimal places in xslt , we need to code like this.

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

if price value is 11.5246956 then it would round to 4 decimal places and result would be 11.5247

if price value is 11.25 then it would round to 4 decimal places and result would be 11.25 not 11.2500

what if we want to round to 4 decimal places and also want to show 11.25 to 11.2500 then need to use the following code.

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

it would round to 4 decimal places and every value that has less then 4 decimal palces will be shown by appending 0 to left. e.g if price value is 11.25 it will show to 11.2500

and some cases we don't need to round value to specific values we just need to truncate it by 4 decimal places , for exapmle  if price value is 11.5246956 and want to show it 11.5246 just up to 4 decimal places but no round then following code can be used.

<xsl:value-of select="format-number(concat(substring-before(price,'.'),'.', substring(substring-after(price,'.'),1,5)),'#.00000')"/>

No comments:

Post a Comment