We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,682 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to Add a Parenthesis to a Number Using Translate

Hi,
I'm trying to add a right parenthesis to a number when it is negative upon returning from a Java function and so far am unable to do it. I can add the left parenthesis no problem but can't find a way to add the right parenthesis. It is much appreciated if anyone can help?
This is the line I'm using to try and include the right parenthesis. I'm thinking the code would interpret the end of the number as a space and translate it to a ')'. I've tried both " " and "" but neither works. An example of the result I currently get is this: (250.00
<xsl:value-of select='translate(translate(format-number(nws:FormatNoDollar4(nws:ReturnTotalUndesignated()),"0.00"),"-","(")," ",")")'/>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:nws="urn:ns"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:fo="http://www.w3.org/1999/XSL/Format" 
xmlns:nwsexport="urn:nsexport">
.
.
.
<xsl:value-of select="nws:ResetCount()"/>
<xsl:apply-templates select="Details/CurrentAct" mode="totalundesigamt"/> 
<table-row font-size='9' font-weight='500'> 
<table-cell><block><xsl:value-of select="Details/CurrentAct/Date"/></block></table-cell> 
<table-cell><block>Payment</block></table-cell> 
<table-cell><block>Credit Applied</block></table-cell>
<table-cell><block text-align='far'>
<xsl:value-of select='translate(translate(format-number(ns:FormatNoDollar4(ns:ReturnTotalUndesignated()),"0.00"),"-","(")," ",")")'/> 
</block></table-cell>
</table-row>
<!--Undesignated Template for Balance Processing for payments and adjustments -->
<xsl:template match="Details/CurrentAct" mode="totalundesigamt">
<xsl:if test="substring(CAType,2,11) = 'ndesignated'">
<table-row font-size='9' font-weight='500'>
<xsl:value-of select='ns:AddUndesignated(number(translate(translate(Amount,"(","-"),")","")))'/>
</table-row> 
</xsl:if>
</xsl:template> 
<!--********************Java Script*************************************** -->
<msxsl:script language="javascript" implements-prefix="nws">
<![CDATA[
var TotalUndesignated=0;
/*For the Undesignated billing data, this function takes the input xml field Amount and totals it. 
If the amount = 0.00 the payment amount is used in the returned amount */ 
function AddUndesignated(inVal) 
{ 
TotalUndesignated=TotalUndesignated+inVal;
var num = new Number(TotalUndesignated);
if(String(num) == '0.00' || num == 0.00)
TotalUndesignated = num+inVal; 
}
function ReturnTotalUndesignated()
{ 
return TotalUndesignated;
}
function ResetCount()
{ 
TotalUndesignated=0;
}
function FormatNoDollar4(inNumber) {
var NumDigitsAfterDecimal = 2;
var UseParensForNegativeNumbers = true;
var GroupDigits = true; 
var num = new Number(inNumber);
if(String(num) == "NaN" || num == 0)
return '0.00';
else
return num;
}
]]>
</msxsl:script>
</xsl:stylesheet>
3
Contributors
4
Replies
6 Days
Discussion Span
9 Months Ago
Last Updated
5
Views
Oracan
Newbie Poster
2 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

I would personally think that the end of the number wouldnt be assumed as a space.

Perhaps you could store the value of

<xsl:value-of select='translate(translate(format-number(ns:FormatNoDollar4(ns:ReturnTotalUndesignated()),"0.00"),"-","(")," ",")")'/>

into a local variable instead and then use a concatenation of the variable and ")" to put the final bracket on?

Its a bit messy but:

    <table-cell>

      <block text-align='far'>
        <xsl:variable name ='tempConcat'>(250.00</xsl:variable>
        <xsl:variable name='tempParanthesis'>)</xsl:variable>

        <xsl:value-of select ='concat($tempConcat, $tempParanthesis)' />

      </block>

    </table-cell>

Used a second variable to store the closing parenthesis as it causes havok within the stylesheet and it also didnt like the HTML code for it. Obviously replace my temporary (250.00 (as provided in your example) with the code that generates it :)

MikeyIsMe
Master Poster
798 posts since Nov 2010
Reputation Points: 88
Solved Threads: 69
Skill Endorsements: 10

Hi all. I applied this code format-number($x, '0.0;(0.0)') in place of the double translate code and it worked. Thanks for the reply Mikey.

Oracan
Newbie Poster
2 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Ah good to hear, mark the thread as solved if no further help is required :)

MikeyIsMe
Master Poster
798 posts since Nov 2010
Reputation Points: 88
Solved Threads: 69
Skill Endorsements: 10

For XML Developement, contact me aT Click Here ,Click Here

gilltech
Newbie Poster
1 post since Jun 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.5318 seconds using 2.72MB