Rob Koch
Rob Koch

Reputation: 1583

Formatting negative values in XSLT

I'm trying to get the correct negative value format displayed:

Right now it is showing (19%), when I want to show it as -19%.

I've got an XML file with

<PerChg>-0.190</PerChg>

Formatting it in XSL doesn't work:

<xsl:value-of select="format-number(PerChg, '#0%')"/>
<xsl:value-of select="format-number(PerChg, '#0%;-#0%')"/>

Even tried:

<xsl:decimal-format name="decimalChangePercent" minus-sign="-" />
<xsl:value-of select="format-number(PerChg, '#0%;-#0%', 'decimalChangePercent')"/>

Any ideas? Am in the .NET world, using XslCompiledTransform/XSLT 1.0.

Thanks!

Upvotes: 1

Views: 4089

Answers (2)

Rob Koch
Rob Koch

Reputation: 1583

Bingo, found the culprit!

There's a JavaScript function that converts all negative values into parentheses.

function MakeNegative() {
                TDs = document.getElementsByTagName('td');
                for (var i = 0; i < TDs.length; i++) {
                    var temp = TDs[i];
                    if (temp.firstChild && temp.firstChild.nodeValue) { // if not null
                        if (temp.firstChild.nodeValue.indexOf('-') == 0) {
                            temp.className += " negative";
                            temp.firstChild.nodeValue = '(' + temp.firstChild.nodeValue.replace('-', '') + ')';
                        }
                    }
                }
            };

Thanks for your confirmation @kjhughes to make me look harder!

Upvotes: 1

kjhughes
kjhughes

Reputation: 111620

Not seeing the problem...

This input XML file,

<PerChg>-0.190</PerChg>

provided to this XSLT,

<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(PerChg, '#0%')"/>
  </xsl:template>

</xsl:stylesheet>

yields this output,

-19%

as expected.

Upvotes: 4

Related Questions