Home > database >  Ms Access Database Field losing decimal right side digits
Ms Access Database Field losing decimal right side digits

Time:01-03

I want to store decimal value into my field when you divide

1120/90 = 12.444444444444444444444444444444

This long but I am losing right side digits, I am only getting 13 right side digits - like that:

12.4444444444444

But when I multiply this back again:

12.4444444444444 x 90 = 1119.999999999996

This is not the correct answer; the correct answer is

12.444444444444444444444444444444 x 90 = 1120

Please help me here.

Thanks

CodePudding user response:

You can use Decimal for this:

? CDec(1120) / CDec(90)
 12.444444444444444444444444444 

? (CDec(1120) / CDec(90)) * 90
 1120 

CodePudding user response:

I banged on this for a long time but couldn't find a way to prove the following. assuming any operands can be cast to the integer type without loss of information (1120 & 90 can) then you usually must do the calculation simultaneously: The floating point division operator / handles integer like operands correctly but returns the type double. chained operations are also handled correctly if done all at once. Hence (1120/90) * 90 is calculated correctly as 1120 but also, typename(1120/90*90) returns the double datatype. if you store the intermediate value 1120/90 you get a double of 12.44444 repeating which isn't quite 1120/90.
My suggestion is to store both the operands rather than reducing them to one number. Then do the calculation all at once.

  • Related