# Division in java double variable.

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.

##### Ananth Chinnaraj replied

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