Here's some code that will help you accomplish rounding to fix decimal places.
All my code may not be needed ,
pick the pieces you need.
public static String format(double source) {
synchronized (cacheLocale) {
updateCache();
return cacheNumberFormat.format(source);
}
}
public static Number parse(String source) {
synchronized (cacheLocale) {
updateCache();
try {
return cacheNumberFormat.parse(source);
} catch (ParseException p) {
return INVALID;
}
}
}
// ============= privates =============
private static void updateCache() {
Locale current = Locale.getDefault();
if (!current.equals(cacheLocale)) {
cacheLocale = current;
cacheNumberFormat = NumberFormat.getInstance(cacheLocale);
cacheNumberFormat.setMaximumFractionDigits(2);
cacheNumberFormat.setMinimumFractionDigits(2);
}
}
public double getDouble(String num) {
if (num==null) return 0.00;
if(num.startsWith("$"))
num = num.substring(1);
if(num.startsWith("N/A"))
num = "0.0";
Number parsedNum = this.parse(num);
return
Double.isNaN(parsedNum.doubleValue())?0.0:parsedNum.doubleValue();
}
public String fixDecimalPlaces(double num) {
return Double.isNaN(num)?"0.00":this.format(num);
}
public static final Double INVALID = new Double(Double.NaN);
private static Locale cacheLocale = new Locale("NONE","","");
private static NumberFormat cacheNumberFormat;
hope that helps
santosh
Alireza Nahavandi <[EMAIL PROTECTED]> on 10/23/2001 09:50:42 AM
Please respond to A mailing list about Java Server Pages specification and
reference <[EMAIL PROTECTED]>
To: [EMAIL PROTECTED]
cc: (bcc: Santosh Daryani/IT/Aon Consulting)
Subject: Rounding in Java
In Java, how can I round a double value with specific precision. Example :
value 1.715 need to be round up to 1.72 and 1.714 need to be round down to
1.71.
Thank you.
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:
http://archives.java.sun.com/jsp-interest.html
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.jsp
http://www.jguru.com/faq/index.jsp
http://www.jspinsider.com
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:
http://archives.java.sun.com/jsp-interest.html
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.jsp
http://www.jguru.com/faq/index.jsp
http://www.jspinsider.com