----- Original Message ----- From: "Josh G" <[EMAIL PROTECTED]> To: "Tomcat Users List" <[EMAIL PROTECTED]> Sent: Thursday, November 14, 2002 7:53 AM Subject: Java q: round to n decimal points?
> Is there a nice easy way to round a double to n decimal points? I couldn't > see anything in Math,Double,String, or NumberFormat... if you want a double result, you can do the following: double round(double value, int numDecimals) { double factor = 1.0; // This calculation could be improved for high numDecimal values while (numDecimals-- > 0) factor *= 10.0; return Math.round(value * factor) / factor; } if you just need it for presentation, it could be enough to have a String representation: String getRounded(double value, int numDecimals) { String template = "0.000000000000000000000000000"; if (numDecimals < 0) numDecimals = 0; String mask = template.substring(0, 2 + numDecimals); java.text.DecimalFormat df = new java.text.DecimalFormat(mask); return df.format(value); } Hope it helps :-) PS: The code is not necessarily bug-free or optimal ;-) > > -Josh > -- > And can you tell me doctor why I still can't get to sleep? > And why the channel 7 chopper chills me to my feet? > And what's this rash that comes and goes, can you tell me what it means? > God help me, I was only 19 > > > > -- > To unsubscribe, e-mail: <mailto:tomcat-user-unsubscribe@;jakarta.apache.org> > For additional commands, e-mail: <mailto:tomcat-user-help@;jakarta.apache.org> > > -- To unsubscribe, e-mail: <mailto:tomcat-user-unsubscribe@;jakarta.apache.org> For additional commands, e-mail: <mailto:tomcat-user-help@;jakarta.apache.org>