No. This is not even a java problem. computers have a hard time holding the EXACT float value you want... but they get pretty close. I don't have the spec handy (and you don't REALLY want to read it anyway) but there is an IEEE standard on how floats are stored.
generally, when using floats, you're going to do your math, then format the result to the rounding you want... or check that the result is within whatever tolerances you consider acceptable. [ October 16, 2008: Message edited by: fred rosenberger ]
Well, you shouldn't be creating any Java variables in a JSP page, from the design point of view.
But 30 BigDecimal objects is a very small number. Computers are fast today, but it wouldn't have been a big number ten years ago either. Try it if you like. You won't notice any difference even if you create 3,000 BigDecimal objects, I predict.
Yeah from a design point of view its bad to put it in a jsp page. But a lot of code is generating the HTML and it would be tedious to do in in a servlet.
In itself, creating numerous objects is not the resource hit a lot of people expect. Behind the scenes, your Java programs are probably creating far more objects than you realize. For example, simply calling Integer.valueOf(0) creates a cache array of 256 new Integer objects.
There would be some performance cost in creating BigDecimal instances. But whether it would be "bad" (noticible) depends more on what you do with these objects.
Originally posted by Sahana Hegde: Yeah from a design point of view its bad to put it in a jsp page. But a lot of code is generating the HTML and it would be tedious to do in in a servlet.
Right. And having a servlet generate HTML is also a bad thing to do, too, from the design point of view. What you should do is to have the servlet organize the data that is to be sent to the client, put that data into request scope, then forward to a JSP that takes the data and writes the HTML containing it. This pattern has been around for at least 5 years, so it surprises me that there are still people who don't use it.
I am generating a report with the data and I calculating in each loop of the result set the data that needs to be generated, and there is a whole lot of calculations with multiple records before I can generate one row in the report What should be the approach then?
Also could you guys suggest some material for reading for best practices in building applications with simple things like using the right data type to some more complex things.
Disadvantage: 10 years old, printed before Swing, the Collections Framework, the invention of Agile Programming and the discovery that Thread#stop doesn't work. Advantage: easy to read, available second-hand for a very reasonable price: S Gilbert, M McCarty, Object-Oriented Design in Java�, Corte Madera CA: Waite Group Press (MacMillan) (1998).
It doesn't however seem to mention the imprecision of floating-point arithmetic. Some of my programming books mention this, eg Deitel and Deitel, 6th edition page 190, and Barry Burd, "Beginning Programming with Java for Dummies" (page 300) has a listing with "Do you have change for 20.338500000000003" written against it.