XSL: format currency amount with decimal format

I'm trying to build an xml such that the amount contains a decimal in appropriate position. I have the following:
      <xsl:variable name="NumberOfDecimals">
        <xsl:choose>
          <xsl:when test="$viewInfo/@NumberOfDecimals">
            <xsl:value-of select="$viewInfo/@NumberOfDecimals"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>2</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>


      <xsl:variable name="MONEY_FORMAT">
        <xsl:choose>
          <xsl:when test="$NumberOfDecimals='0'">
            <xsl:value-of select="'###0'"/>
          </xsl:when>
          <xsl:when test="$NumberOfDecimals='1'">
            <xsl:value-of select="'###0.0'"/>
          </xsl:when>
          <xsl:when test="$NumberOfDecimals='2'">
            <xsl:value-of select="'###0.00'"/>
          </xsl:when>
          <xsl:when test="$NumberOfDecimals='3'">
            <xsl:value-of select="'###0.003'"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="'###0.00'"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
	  
      <Total>
	  
        <xsl:attribute name="DecimalPlaces">
          <xsl:value-of select="$NumberOfDecimals"/>
        </xsl:attribute>


        <xsl:attribute name="Amount">
          <xsl:value-of select="format-number($viewInfo/@Total, $MONEY_FORMAT)"     />  <!--$viewInfo/@Total = 600 -->
        </xsl:attribute>
      </Total> 

Open in new window

      
        
However, this returns
<Total DecimalPlaces="2" Amount="600.00" />
        
        
I am expecting 6.00
badtz7229Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gertone (Geert Bormans)Information ArchitectCommented:
format-number() does not change the value of the integer,
it only changes the way it looks
(it is a way to "format" an integer to a string, without changing its value)
badtz7229Author Commented:
understood.

but what am i doing wrong when I am specifying the format of the original amount (600) to look like 6.00  ?
Gertone (Geert Bormans)Information ArchitectCommented:
you are only specifying the format.
If you actually need to divide by 100 first, you need to add a div statement

Can you briefly explain what exactly you need?
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

badtz7229Author Commented:
i am given the amount with no decimals.
i am given the numberofdecimals attribute.

i want to format the amount using the numberofdecimals value.
i.e.
Total= 600
NumberOfDecimals =2

I want Amount = 6.00

or if
Total= 1000
NumberOfDecimals =2

I want Amount = 10.00
Gertone (Geert Bormans)Information ArchitectCommented:
So, you don't need to format, you need to change the number
give me a minute
Gertone (Geert Bormans)Information ArchitectCommented:
Try this

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    
    <xsl:variable name="zeroes"><xsl:text>000000000000000000000000000</xsl:text></xsl:variable>
    
    <xsl:template match="viewInfo">
        <xsl:variable name="viewInfo" select="."></xsl:variable>
        
 
        <xsl:variable name="NumberOfDecimals">
            <xsl:choose>
                <xsl:when test="$viewInfo/@NumberOfDecimals">
                    <xsl:value-of select="$viewInfo/@NumberOfDecimals"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>2</xsl:text>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        
        <xsl:variable name="zero-sub" select="substring($zeroes, 1, $NumberOfDecimals)"/>
        <xsl:variable name="divider" select="number(concat('1', $zero-sub))"/>
        <xsl:variable name="MONEY_FORMAT" select="concat('###0.', $zero-sub)"/>
 
        <Total>
            <xsl:attribute name="DecimalPlaces">
                <xsl:value-of select="$NumberOfDecimals"/>
            </xsl:attribute>
            <xsl:attribute name="Amount">
                <xsl:value-of select="format-number($viewInfo/@Total div $divider, $MONEY_FORMAT)"     />  <!--$viewInfo/@Total = 600 -->
            </xsl:attribute>
        </Total> 
    </xsl:template>
    
    
    
    
</xsl:stylesheet>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gertone (Geert Bormans)Information ArchitectCommented:
I used this XML for testing

<viewInfo NumberOfDecimals="5" Total="600"></viewInfo>

Open in new window

badtz7229Author Commented:
i ran that xsl with input xml but i recevied:

<Total DecimalPlaces="5" Amount="0.00600" />
badtz7229Author Commented:
nvmd, i changed the DecimalPlaces=2
badtz7229Author Commented:
what is the purpose of the

xsl:variable name="zeroes"><xsl:text>000000000000000000000000000</xsl:text></xsl:variable ?
Gertone (Geert Bormans)Information ArchitectCommented:
yes, it is set to 5 decimals
works fine, no?
Gertone (Geert Bormans)Information ArchitectCommented:
that variable is a long string of zeroes
I use substring to take just enough zeroes to construct the format string and the divider

<xsl:variable name="zero-sub" select="substring($zeroes, 1, $NumberOfDecimals)"/>
will be '00' when $NumberOfDecimals = 2
will be '000' when $NumberOfDecimals = 3
badtz7229Author Commented:
Thanks.this worked.
Gertone (Geert Bormans)Information ArchitectCommented:
welcome
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
CSS

From novice to tech pro — start learning today.