Solved

zero after decimal point dropped when no number before decimal point, .06 cents becomes .60 cents

Posted on 2003-10-21
9
389 Views
Last Modified: 2012-06-21
When I transform my XML to HTML, I lose the zero after the decimal point, IF, there is no number before the decimal point.

In the XML                  In the HTML

1.06                        1.06
0.06                        0.6
.06                        0.6

My XSL

<xsl:template match="value">
<xsl:choose>
  <xsl:when test=".!=''">
    <xsl:choose>
      <xsl:when test="number(.)=number(.)">
        <xsl:value-of select="format-number(.,'###,##0.0###')" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="." />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:when>

I have tried (.,'###,##0.####')
and      (.,'###,##0.00##'),    etc.,  with no luck.    6 cents always ends up as 60 cents.
0
Comment
Question by:rcheney
  • 4
  • 3
  • 2
9 Comments
 
LVL 4

Expert Comment

by:Xikilm
ID: 9591032
Hi rcheney:

What are you using for your parser?  I passed the above numbers and stylesheet through Xalan, and it returns the correct results.



-Xikilm
0
 

Author Comment

by:rcheney
ID: 9591519
I am using Altova, the "XML Spy" parser.
0
 
LVL 4

Expert Comment

by:Xikilm
ID: 9591587
Just did a quick search and found this:

http://www.altova.com/Fixed_Defects.html
3573 | XSLT format-number removes leading 0 from decimal part of value | Spy Ent | AltovaXSLT

Sounds like its a known problem that is fixed in "Release 3".  HTH.


-Xikilm
0
 
LVL 2

Expert Comment

by:weareu
ID: 9593242
It is a known problem and it was supposedly fixed in release 3 but it still doesn't seem to always work correctly... I suggest just writing an ugly little hack which check if the value starts with either 0 or '.' and then after formatting just selecting the value of the first part (before the '.') + a '.0' and then the value after the '.'. Quite nasty but successful.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:rcheney
ID: 9593570
You are right.  They either didn't fix it, or it was working correctly and they broke it.  Anyway I agree the nasty hack is the way to go.  Could I trouble you for a little help or just point me in the right direction.  I started the hack by finding the decimal point and zero like you said <xsl:when test="contains(number(.),'.0')">  but how do I split the number and deal with the before and after the decimal point parts?

<xsl:template match="value">
<xsl:choose>
<xsl:when test=".!=''">
<xsl:choose>
<xsl:when test="number(.)=0">0</xsl:when>
<xsl:when test="number(.)=number(.)">
<xsl:choose>
<xsl:when test="contains(number(.),'.0')">

Here I find the decimal point zero but how do I split the before and after and format separately??

</xsl:when>
</xsl:choose>
<xsl:value-of select="format-number(.,'###,##0.0###')"/></xsl:when>
0
 

Author Comment

by:rcheney
ID: 9593705
I think I need to make a parameter and then create the before and after variables.

<xsl:param name="numwithdecimal" select="number(.)"/>
<xsl:variable name="beforedecimal" select="substring-before(numwithdecimal, '.')"/>
<xsl:variable name="afterdecimal" select="substring-before(substring-after(numwithdecimal, '.'),'.')"/>

and then (after formatting the separate pieces) put the result back together like below

<xsl:when test="contains(number(.),'.0')">
<xsl:value-of select="beforedecimal"/> AND <xsl:value-of select="afterdecimal"/>

    How do I format the separate pieces?
0
 
LVL 2

Accepted Solution

by:
weareu earned 125 total points
ID: 9593922
<xsl:template match="value">
<xsl:choose>
  <xsl:when test=".!=''">
    <xsl:choose>
      <xsl:when test="number(.)=number(.)">
         <xsl:choose>
            <xsl:when test="starts-with(.,'.') or starts-with(.,'0')">
                  <xsl:value-of select="format-number(number(substring-before(.,'.')),'###,###')"/>.0<xsl:value-of select="format-number(number(substring-after(.,'.')),'##')"/>
             </xsl:when>
             <xsl:otherwise>
                <xsl:value-of select="format-number(.,'###,##0.0###')"/>
             </xsl:otherwise>
         </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="." />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:when>
  <xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:template>

couldn't really test this, but see if it works or if you could rip it appart and make it work...
0
 

Author Comment

by:rcheney
ID: 9594061
Thanks that looks great.  I haven't had time to test it yet but it gets me close enough so I can tweak it, if necessary.   A big thanks to wearu and  Xikilm.
0
 
LVL 4

Expert Comment

by:Xikilm
ID: 9594066
When I run ".06" through it returns a "NaN.06".  Not sure if you're having similar problems with your parser.  If so, you can fix it with:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:decimal-format name="test" NaN="0"/>
    <xsl:template match="value">
<xsl:choose>
  <xsl:when test=".!=''">
    <xsl:choose>
      <xsl:when test="number(.)=number(.)">
         <xsl:choose>
            <xsl:when test="starts-with(.,'.') or starts-with(.,'0')">
                  <xsl:value-of select="format-number(number(substring-before(.,'.')),'###,###','test')"/>.0<xsl:value-of select="format-number(number(substring-after(.,'.')),'##')"/>
             </xsl:when>
             <xsl:otherwise>
                <xsl:value-of select="format-number(.,'###,##0.0###')"/>
             </xsl:otherwise>
         </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="." />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:when>
  <xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>


-Xikilm
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now