views:

423

answers:

2

Hi, first time SO user :)

I know that I can format a number like this:

format-number($value, '###,###.00')

But I would like to remove the dot and the zeroes if $value is zero.

So,

37368 -> 37,368
2.666667 -> 2.66

Is this possible using just number formatting (doesn't seem like it) or do I have to do something along the lines of

if (int(value) == value ) {...}

Thanks for any help,

/Anders

A: 

xpath 2.0 contains conditional logic if..then..else but in the more likely case that you're in xpath 1.0 I'm afraid you'll have to either rely on XSLT to do this for you, i.e:

<xsl:variable name="myvar">
  <xsl:choose>
    <xsl:when test="$value > 0">
      <xsl:select="format-number($value, '###,###.00')"/>
    </xsl:when>
    <xsl:otherwise>0</xsl:otherwise>
  </xsl:choose>>
</xsl:variable>

..or mixin an extension (.NET can do this natively, EXSLT exists otherwise, probably more options available)

To the best of my knowledge no xpath functions exist to get you out of this, but I could be missing something exotic.

annakata
Actually, this is for a log that will be used internally in the company. I can pretty much assume that everyone uses the lastest version of their browser. So I assume that XSLT/XPath 2.0 is ok then?
Srekel
+1  A: 
format-number($value, '###,###.##')
Azat Razetdinov
Hmmm. I more or less misread the OP's condition on non-zero numbers (I thought they needed .00) - For the mentioned case I think this is the correct answer.
annakata
Srekel probably has mistaken “$value is integer” for “$value is zero”.
Azat Razetdinov
Thanks, this works! Although, I actually wanted format-number($value, '###,##0.##')So that 0.33 isn't shown as ".33".
Srekel