• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Formatting Servlet output (double precision) - Help Required.

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out the DecimalFormat class in java.text. package.
Bosun
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



------------------
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Right! We're on it! Let's get to work tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic