title: XSLT Decimal formatting based on input XML
body: <p>I have a situation where my XSLT files should display prices alongwith decimals conditionally, depending on whether the input XML contains decimals or not. So, I can receive XML files with two types of values - XML will contain all prices formatted with decimals upto two places (I call this one "Decimal-XML") or the prices will be rounded off to the nearest integer (I call this one "Integer-XML").</p>
  
  <p>My problem is that I need to refactor as little as possible in the XSLT files and yet allow them to apply the transformation to XHTML in the same format as the XML input. In order to accomplish this, I implemented and suggested three guidelines to my team:</p>
  
  <ol>
  <li>Remove all <code>format-number()</code> function calls when the value is being computed for calculations or stored in a variable. Use <code>number(&lt;value&gt;)</code> instead. However, certain conditions apply to this rule (See below).</li>
  <li>When the value is to be displayed, use the <code>format-number(&lt;value&gt;, '#.##')</code> format. This should ensure that integer or decimal values will be displayed as originally present in the XML. </li>
  <li>For optional tags (such as "Discount"), use the <code>format-number(&lt;value&gt;, '0.00')</code> function even if the value is only being computed. This is necessary because if the tag is absent, trying to obtain a value will give an <code>NaN</code> result.</li>
  </ol>
  
  <p>Here is a illustrative example of the XSLT:</p>
  
  <pre><code>  &lt;x:stylesheet version="1.0" xmlns:x="http://www.w3.org/1999/XSL/Transform"&gt;
    &lt;x:template match="/"&gt;
      &lt;html&gt;
        &lt;body&gt;
          &lt;table border="1" width="60%"&gt;
            &lt;tr&gt;
              &lt;th&gt;Simple&lt;/th&gt;
              &lt;th&gt;number()&lt;/th&gt;
              &lt;th&gt;format-number(&amp;lt;expression&amp;gt;, '0.00')&lt;/th&gt;
              &lt;th&gt;format-number(&amp;lt;expression&amp;gt;, '#.##')&lt;/th&gt;
            &lt;/tr&gt;
            &lt;x:apply-templates /&gt;
          &lt;/table&gt;
        &lt;/body&gt;
      &lt;/html&gt;
    &lt;/x:template&gt;
  
    &lt;x:template match="Item"&gt;
      &lt;x:variable name="qty" select="number(@numItems)" /&gt;
      &lt;x:variable name="cost" select="number(ItemCost) * $qty" /&gt;
      &lt;x:variable name="extraCharges" select="(number(Tax) + number(TxnFee)) * $qty"/&gt;
      &lt;x:variable name="discount" select="format-number(Discount, '0.00') * $qty"/&gt;
      &lt;tr&gt;
        &lt;td&gt;
        &lt;!-- Works for Integer-XML, but values in Decimal-XML are
        *sometimes* rendered upto 14 decimal places. Even though Quickwatch
        shows it correctly in the debugger in VS. I cannot figure out what's
        special about the error cases. --&gt;
          &lt;x:value-of select="$cost + $extraCharges - $discount"/&gt;
        &lt;/td&gt;
        &lt;td&gt;
          &lt;!-- Works same as the above case. --&gt;
          &lt;x:value-of select="number($cost + $extraCharges - $discount)"/&gt;
        &lt;/td&gt;
        &lt;td&gt;
        &lt;!-- Works for Decimal-XML, but values in Integer-XML are always
        rendered with decimal digits. --&gt;
          &lt;x:value-of select="format-number(($cost + $extraCharges - $discount), '0.00')"/&gt;
        &lt;/td&gt;
        &lt;td&gt;
        &lt;!-- Works for Integer-XML, but some values in Decimal-XML are
        rendered incorrectly. For example, 95.20 is rendered as 95.2;
        95.00 is rendered as 95 --&gt;
          &lt;x:value-of select="format-number(($cost + $extraCharges - $discount), '#.##')"/&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/x:template&gt;
  &lt;/x:stylesheet&gt;
  </code></pre>
  
  <p>As the HTML comments note, it works in most cases but not all.</p>
  
  <p>I would like to use a single expression to format all prices same as the input, rather than applying "when-otherwise" constructs everywhere which would additionally require a boolean parameter passed to the XSLT to determine the display format. The present state of the XSLT is that all numbers are rounded off regardless of the input, using <code>format-number(&lt;expression&gt;, '0')</code>.</p>
  
  <p>How do I accomplish this?</p>
  
  <hr>
  
  <p><strong>Edit:</strong> After Dimitre's comment, I decided to create a sample XML (and the XSLT above) so the experts can try it out easily.</p>
  
  <p>Sample XML (This one contains decimals):</p>
  
  <pre><code>&lt;ShoppingList&gt;
    &lt;Item numItems="2"&gt;
      &lt;ItemCost&gt;10.99&lt;/ItemCost&gt;
      &lt;Tax&gt;3.99&lt;/Tax&gt;
      &lt;TxnFee&gt;2.99&lt;/TxnFee&gt;
      &lt;Discount&gt;2.99&lt;/Discount&gt;
    &lt;/Item&gt;
    &lt;Item numItems="4"&gt;
      &lt;ItemCost&gt;15.50&lt;/ItemCost&gt;
      &lt;Tax&gt;5.50&lt;/Tax&gt;
      &lt;TxnFee&gt;3.50&lt;/TxnFee&gt;
      &lt;Discount&gt;3.50&lt;/Discount&gt;
    &lt;/Item&gt;
  &lt;/ShoppingList&gt;
  </code></pre>