aspose file tools
The moose likes JSP and the fly likes A question about formatNumber and decimal places Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "A question about formatNumber and decimal places" Watch "A question about formatNumber and decimal places" New topic
Author

A question about formatNumber and decimal places

Thomas Kennedy
Ranch Hand

Joined: Jan 20, 2008
Posts: 137
I'm printing currency values to the screen using <fmt:formatNumber>. If my numeric value has a non-zero fractional part I want exactly two decimal places. If the fractional part is zero I want the decimal places omitted.

so 45.75 prints as $45.75
but 201.00 prints as $201

How can I do that?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 19357
    
  10

So that means that 18.30 should print as $18.30 and not as $18.3? In that case I think you're going to need two different formatters and some kind if-then-else to distinguish the two cases. Writing a custom tag might be the right way to go here.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 62279
    
  68



Why two formatters? What are the two cases? I only see one.

No custom tag is necessary, the <fmt:formatNumber> tag should do just fine.

But the OP hasn't shown that any effort has been put into the problem yet. What formats have been tried that aren't working?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 19357
    
  10

Thomas Kennedy wrote:If my numeric value has a non-zero fractional part I want exactly two decimal places.

If the fractional part is zero I want the decimal places omitted.


These are the two cases I saw. I don't see how to implement that with a single DecimalFormat.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 62279
    
  68

Ah. I thought that no-trailing was an undesirable outcome.

In that case, I'd simply use the ternary operator to choose between the two patterns.

Thomas Kennedy
Ranch Hand

Joined: Jan 20, 2008
Posts: 137
Is there anything in the fn library that can perform decision in a robust way? All I can think of is to check if the String representation of the value ends with ".00", and that sounds ugly. BTW does Oracle have a documentation page for EL? Thank you.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 19357
    
  10

Expression Language Specification Version 2.1
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 19357
    
  10

And no, there isn't an EL function in the fn: namespace which does that. But you don't need one, either. One of the EL operators is sufficient.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 62279
    
  68

What type is the numeric value? How would you test it in a Java expression to see if it were a whole number or not?
Thomas Kennedy
Ranch Hand

Joined: Jan 20, 2008
Posts: 137
In Java I would use Math.round() to compare the value to itself. So I could step out to a scriptlet but would prefer to avoid that.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 62279
    
  68

OK, so let's say you've been forbidden to use the Math class. How would you do with basic operators?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: A question about formatNumber and decimal places