Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format-Number 1

Status
Not open for further replies.

MikeAJ

Programmer
May 22, 2002
108
0
0
US
I need to format currency such that:
1234.56 = 1,234.56
12345.6 = 12,345.60
123456 = 123,456

I'm using format-number(@price, '###,###.00'), but it leaves on .00. Is there a pattern to drop the decimal when it's .00?

Thanks!
 
I did, and with cooktop it doesn't meet the second use case. I also tried to use translate to drop it off, but that wasn't working. translate(format-number(num, '###,###.00'), '.00', '')

Thanks,
Mike

 
You need two incompatible formats in format-number(), hence, you have to test the number to determine which to use.

[1] Script a named template, say, "format-number-ext"
[tt]
<xsl:template name="format-number-ext">
<xsl:param name="val" />
<xsl:choose>
<xsl:when test="$val mod 1 = 0">
<xsl:value-of select="format-number($val,'###,###')" />
</xsl:when>
<xsl:eek:therwise>
<xsl:value-of select="format-number($val,'###,###.00')" />
</xsl:eek:therwise>
</xsl:choose>
</xsl:template>
[/tt]
[2] Use it when needed.
[tt] [green]<!--
Replacing every occurence of this, for instance
<xsl:value-of select="format-number(@price,'###,###.00')" />
by the following line.
-->[/green]
<xsl:call-template name="format-number-ext">
<xsl:with-param select="@price" />
</xsl:call-template>
[/tt]
 
Thanks tsuji and Tom! tsuji, your template works great. Thanks again for the help!

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top