• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XSLT format-number() issue

 
Sarah Shay
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to format lets say a quantity - In this format number mask I add in any thousand comma sperators that are required. My issue is the numbers after the decimal place. Sometimes I can get 112.8500 returned in the XML. other times I can get 12.54. I can get up to 5 decimal places. So if I pass it through the format-number #,##0.00000 I get trailing zeros added on even if they are not returned in the XML. This is not acceptable. If I try use #,##0.#####, any trailing zeros that are returned in the XML(it does happen), these are knocked off. Any ideas on what sort of a format-number I could use to handle my scenarios?

Any help will be much appreciated,

Sarah
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sarah,

I am a little confused as to what format you want ?
Are you saying you want two digits or four or five digits after the decimal ?
Or are you saying all these are acceptable ?

Could you please re-phrase the question.
regds.

- m
 
Sarah Shay
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Im sorry I didnt make it clear.

The number of digits after the decimal place can vary from 2 to 5. So if I get 3 decimal places returned my template is adding on an additional 2 zeros at the end of it to make it 5 decimal places in length. If I use a template such as ###.#####, this will not add on zeros but the issue with this is that if the number returned to me is 23.450, this template will cut off the zero at the end.

Have I made my problem any clearer??
Thanks
Sarah
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somewhat...but let me try to rephrase -

Your input may have 2 to 5 digits in the decimal portion.
But you want to use a formater in a template so that your output always contains 5 digits after the decimal.

Did I get the problem statement correct ?

- m
 
Sarah Shay
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

No I dnt always want 5 decimal places after the decimal point. I want to display as many digits as is returned in teh XML. My problem is that the format-number func that Im using will always give me 5 decimal places when sometimes I wont want this ie in cases where the XML returns only 3 decimal places
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If that's the case, why use a format-number function ?
Thanks.

- m
 
Sarah Shay
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I need to use it to add in my comma seperators. Dollar sign I could do without the format-number func but thousand seperators I cant
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks much for clarifying. I got it now. Life is strange...you never get a function that does the right thing. phew!

Anyways, I have a few thoughts, see if this solves the problem.
  • get the decimal part.
  • count the number of digits, should be a max of 5. (right?)
  • input that count as the third argument for the format-number function.


  • <!-- assume your input number is stored in the variable myNum -->
    xsl:variable name="decimalPart" select="substring-after($myNum, '.')"/>

    <!-- count the number of digits in the decimalPart -->
    format-number($myNum, 'your-thousand-seperator-format', $count)

    Your homework, figure out how to count the digits and let us know. I would like to learn that - drawing a blank right now. Thanks.
    Hope this helps.

    - m
     
    Madhav Lakkapragada
    Ranch Hand
    Posts: 5040
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Madhav Lakkapragada:

    .....
    ...figure out how to count the digits and let us know. I would like to learn that - drawing a blank right now. Thanks.

    - m


    arrrggggg! One of those moments yesterday. What was I thinking.......

    Just use the string-length function to get the number of digits.

    format-number($myNum, 'your-thousand-seperator-format', string-length($decimalPart))

    That ought to do it. Nice problem.
    Thanks.

    - m
    [ February 04, 2005: Message edited by: Madhav Lakkapragada ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic