wood burning stoves 2.0*
The moose likes JSP and the fly likes BigDecimal in EL addess scales Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "BigDecimal in EL addess scales" Watch "BigDecimal in EL addess scales" New topic
Author

BigDecimal in EL addess scales

Ali Khalfan
Ranch Hand

Joined: Nov 03, 2007
Posts: 126
Hi,

I'm trying to generate a report that has several floating values.

In order to get it precise, I used BigDecimal for all the variables.

I found out that EL in jsp would use A.multiply(B) when saying something like:



and both A and B are BigDecimal

The problem is that A.multiply(B) means that the scales will be added. So, if A has a scale of 2 and B has a scale of 2 I will get a scale of 4 !

So if A = 2.00 and B = 3.00 and I do ${A*B} i'll get 6.0000

Is there any way I can get only 6.00, I want exactly the same decimal places (two) not more not less

Thanks,
[ March 15, 2008: Message edited by: Bear Bibeault ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I'd suggest creating a JavaBean to handle these operations and putting the bean in request scope. For example, you could have a MyData bean with a getAMultB() method that returns a BigDecimal with the correct scope. In your jsp, you'd use the expression ${MyBean.aMultB}.


Merrill
Consultant, Sima Solutions
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

Or check out the <fmt:formatNumber> action.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ali Khalfan
Ranch Hand

Joined: Nov 03, 2007
Posts: 126
Merrill,

It sounds like a good idea but,
I'm going to need to set the values in the bean itself

so if I have variables A and B I'd have to set some bean properties

like



and then retreive the bean

won't that increase the amount of code as I have to substitute 1 line for 3 ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

<cricket> <cricket> <cricket>
Ali Khalfan
Ranch Hand

Joined: Nov 03, 2007
Posts: 126
Bear,

Can you help me out...I think I got your idea about numberFormat, can you show me a sample...everytime I do this:

<fmt:formatNumber value="${factor *1} />

I get an error that this taglib doesnot accept expressions
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

Originally posted by Ali Khalfan:
I get an error that this taglib doesnot accept expressions
That is usually an indication that you are using the wrong JSTL version for the version of JSP that you are using.

What container are you using? (forum instructions dictate that you should have already supplied this info)
Ali Khalfan
Ranch Hand

Joined: Nov 03, 2007
Posts: 126
i sincerely apologize.

I'm using nebeans 5.5, java 6 and JSTL 1.1
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

Gee, you kinda told me everything except what I need to know. Netbeans is your IDE, not your container. I imagine you're using some version of Tomcat. Which one?

If it's Tomcat 5 or 6, you should make sure to set your web.xml up as outlined in the JSP FAQ to enable Servlets 2.4 and JSP 2.0, and then be sure that you are using the correct URIs for JSTL 1.1.
[ March 15, 2008: Message edited by: Bear Bibeault ]
Ali Khalfan
Ranch Hand

Joined: Nov 03, 2007
Posts: 126
sorry, I don't know what's gotten into me these days, I'm not able to think right.

It's tomcat 5..i'll check the FAQ
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

Be sure to check your JSTL URIs as well. The error you are getting usually crops up when you use JSTL 1.0 URIs with JSP 2 or vice versa.
[ March 15, 2008: Message edited by: Bear Bibeault ]
Ali Khalfan
Ranch Hand

Joined: Nov 03, 2007
Posts: 126
works good and the night is young

thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: BigDecimal in EL addess scales