Two Laptop Bag*
The moose likes Servlets and the fly likes Formatting Servlet output (double precision) - Help Required. 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 » Java » Servlets
Bookmark "Formatting Servlet output (double precision) - Help Required." Watch "Formatting Servlet output (double precision) - Help Required." New topic
Author

Formatting Servlet output (double precision) - Help Required.

Suresh Selvaraj
Ranch Hand

Joined: Nov 14, 2000
Posts: 104
Hi,
We have a Form that accepts many fields for user to Input.
Here is the problem...
Example: Assume 2 fields qty (for getting quantity from the user) and unitprice ( accepts unit price from the user).
Inside the servlet...
String unitprice = req.getParameter("unitprice");
double unitprc = Double.valueOf(unitprice).doubleValue();
String qty = req.getParameter("qty");
int quantity = Integer.parseInt(qty);
double total=0.00
total = quantity * unitprc;
When this total field is printed, it gives the output with single precision (decimal) like 25.0 etc.
Here is an example: If qty=5 and unitprice=4.0 or 4.00 or 4
the total reflects only 20.0
Actaully, the output is expected to be something like 20.00 (i.e with 2 decimal places)
Any help is appreciated.
Thanks,
Suresh Selvaraj

Suresh Selvaraj, (author of JQuiz)<br />SCJP2<br /><a href="http://www.decontconsulting.com" target="_blank" rel="nofollow">www.decontconsulting.com</a>
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Check out the DecimalFormat class in java.text. package.
Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Mohamed Yousuff
Ranch Hand

Joined: Jun 23, 2001
Posts: 73

I think it is due to multiplication of int and double.
Try as
total = (double) quantity * unitprc
Originally posted by Suresh Selvaraj:
Hi,
We have a Form that accepts many fields for user to Input.
Here is the problem...
Example: Assume 2 fields qty (for getting quantity from the user) and unitprice ( accepts unit price from the user).
Inside the servlet...
String unitprice = req.getParameter("unitprice");
double unitprc = Double.valueOf(unitprice).doubleValue();
String qty = req.getParameter("qty");
int quantity = Integer.parseInt(qty);
double total=0.00
total = quantity * unitprc;
When this total field is printed, it gives the output with single precision (decimal) like 25.0 etc.
Here is an example: If qty=5 and unitprice=4.0 or 4.00 or 4
the total reflects only 20.0
Actaully, the output is expected to be something like 20.00 (i.e with 2 decimal places)
Any help is appreciated.
Thanks,
Suresh Selvaraj


------------------
Julio Lopez
Greenhorn

Joined: Nov 14, 2000
Posts: 28
Looks like I am the java.math.BigDecimal advocate of the day. Try this,
try {
java.math.BigDecimal unitPriceBigDecimal = new java.math.BigDecimal(req.getParameter("unitprice"));
java.math.BigDecimal quantityBigDecimal = new java.math.BigDecimal(req.getParameter("qty"));
java.math.BigDecimal totalBigDecimal = unitPriceBigDecimal.multiply(quantityBigDecimal);
totalBigDecimal = totalBigDecimal.setScale(2, java.math.BigDecimal.ROUND_HALF_UP);
}
catch (NumberFormatException e) {
}
catch (Exception e) {
}
Julio Lopez
M-Group Systems
Suresh Selvaraj
Ranch Hand

Joined: Nov 14, 2000
Posts: 104
Hi All,
Thanks very much for your reply. I tried using java.text.DecimalFormat class and I was able to get the required output.
Thanks very much.
Suresh Selvaraj
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Formatting Servlet output (double precision) - Help Required.
 
Similar Threads
another newbie needs help...:-(
parameter passing (very beginner)
How to store multiple items into a shopping cart?
How do I use a (boolean?) token to reject user input?
Class Help!