wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes XSLT format-number() issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XSLT format-number() issue" Watch "XSLT format-number() issue" New topic
Author

XSLT format-number() issue

Sarah Shay
Greenhorn

Joined: Jan 04, 2005
Posts: 14
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

Joined: Jun 03, 2000
Posts: 5040
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


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Sarah Shay
Greenhorn

Joined: Jan 04, 2005
Posts: 14
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

Joined: Jun 03, 2000
Posts: 5040
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

Joined: Jan 04, 2005
Posts: 14
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

Joined: Jun 03, 2000
Posts: 5040
If that's the case, why use a format-number function ?
Thanks.

- m
Sarah Shay
Greenhorn

Joined: Jan 04, 2005
Posts: 14
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

Joined: Jun 03, 2000
Posts: 5040
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

    Joined: Jun 03, 2000
    Posts: 5040
    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 ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: XSLT format-number() issue
     
    Similar Threads
    Formatting a String, which contains a number
    A question about formating-number:
    XSLT format number
    Text Formatting
    Please help with a simple GUI.