Java q: round to n decimal points?

2002-11-13 Thread Josh G
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...

-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:   
For additional commands, e-mail: 




Re: Java q: round to n decimal points?

2002-11-14 Thread Carsten Ziegert
It's quite easy:
multiply your double with 10^n, do a simple round, and divide
it by 10^n

Carsten


Am Donnerstag, 14.11.02, um 07:53 Uhr (Europe/Berlin) schrieb Josh G:


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...

-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:   

For additional commands, e-mail: 




--
Hannover Medical School 			University of Applied Sciences
Dept. of Hematology and Oncology		Faculty of Information Sciences
Carl-Neuberg-Straße 1Ricklinger Stadtweg 120
30625 Hannover	30459 Hannover
http://summit-bmt.fh-hannover.de



--
To unsubscribe, e-mail:   
For additional commands, e-mail: 




Re: Java q: round to n decimal points?

2002-11-14 Thread Rodrigo Ruiz

- 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.000";

  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>




Re: Java q: round to n decimal points?

2002-11-14 Thread Sam Seaver
Ya, you have to create a DecimalFormat, and then set it with 2 decimal
places, and then create a string, with the double as the parameter, where
the string will be the 'parsed' double.

S


- Original Message -
From: "Josh G" <[EMAIL PROTECTED]>
To: "Tomcat Users List" <[EMAIL PROTECTED]>
Sent: Wednesday, November 13, 2002 10:53 PM
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...
>
> -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>




RE: Java q: round to n decimal points?

2002-11-17 Thread Hari Yellina
in NuberFormat, Please use the fucntion setMaximumDecimalPoints and Miminum
Decimal Points.

-Original Message-
From: Josh G [mailto:[EMAIL PROTECTED]]
Sent: Thursday, November 14, 2002 05:54 PM
To: Tomcat Users List
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...

-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:[EMAIL PROTECTED]>
For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>