on 05-29-2006 2:29 PM
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.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
84 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.