Skip to main content

numbers of decimal

3 replies [Last post]
adnutri
Offline
Joined: 2008-10-28
Points: 0

Hi experts,

I don't know if this forum is apropriate to my doubt..

But...

I have a simple problem.
Look to help me..

I do a calcution in my application to discovery the IMC of the patient, but how the field is type double, then the numbers of decimal is very big. Example: 23.51231519283718

I want to limit to 2 decimals. I like this result 23.51 like the function ROUND of SQL.

I try use the class numberFormat, but this function is to J2SE, I don't have success when I try import in my class.

Can you help me?

Thanks a lot,

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
sfitzjava
Offline
Joined: 2003-06-15
Points: 0

This might work for you.

double dval = 23.5213533234;
int tmp = (dval*100);
dval = (tmp/100);

Warning: large whole numbers may cause bad things to happen. Maybe use long instead of int for larger whole numbers.

-Shawn

adnutri
Offline
Joined: 2008-10-28
Points: 0

Hey Shawn,

Thank you for your answer..

But in the second line:
int tmp = (dval*100);

Happened the error: "possible loss of precision"

Please, Do you have other solution?

Thanks

sfitzjava
Offline
Joined: 2003-06-15
Points: 0

Type cast it.

Actually you will need 2 type casts to make it work how you need it.

like so:
double dval = 23.5213533234;
int tmp = (int)(dval*100);
dval = ((double)tmp)/100;