cancel
Showing results for 
Search instead for 
Did you mean: 

Division in java double variable.

former_member181928
Participant
0 Kudos

Hi all

I have a simple query .I am using the following code in my java mapping.

Please follow the following java code

double d1 = 22058310.53;

System.out.println("Output Value = " + d1/1000);

This gives me an output

Output Value = 220583.10530000002

whereas the correct o/p should be 220583.1053

Can anyone pls provide a solution to get the correct result.

regards

NIlesh Taunk.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Nilesh,

Use the following code to Divide by 1000 using BigDecimal class which is better than using decimal type.

<b>BigDecimal num= new BigDecimal("22058310.53");

BigDecimal result= num.movePointLeft(3);

System.out.println("Output Value = " + result.toString());</b>

OR

<b>BigDecimal num= new BigDecimal("22058310.53");

BigDecimal result= num.divide(num,BigDecimal.ROUND_HALF_UP);

System.out.println("Output Value = " + result.toString()); </b>

Hope this helps.

Regards,

Ananth

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi Nliesh,

This is 'normal' as double is a floating point data type which is not suitable for exact calculations.

I'm not a java expert but you should use something like big decimal type for your variable d

rgds

Dirk