0

i need to "chop off" a 'double' so that he'll have 3 digits after the dot (not rounding, just cut everything else off).

Here is my code:

double multiply_by = (pow(10.0, _digits_percision));
_number = (_number * multiply_by);
_number = (int) _number;
_number = (_number / multiply_by);

When i'm inputing 1.001, my output (after performing this code) is 1. When my input is 1.007, my output is 1.006. It happens only with 1.xxx Any ideas? Thanks.

amitizle
  • 921
  • 5
  • 11

2 Answers2

2

Your computer is using a base 2 floating point representation. When you input 1.007, the computer converts this to the closest possible representation. It cannot represent 1.007 exactly. The representation is instead something like 1.0069999999999999. Multiply by 1000 and you get 1006.9999999999999. Converting to an int yields 1006.

BTW, this isn't going to happen just with numbers of the form 1.xxx. You just haven't tried enough cases.

Addendum
We had a very similar question today. Rather than repeating my answer here, take a look at my answer to Returning double with precision .

Community
  • 1
  • 1
David Hammen
  • 32,454
  • 9
  • 60
  • 108
1

Start by reading about floating point numbers. If you want to truncate a number to N decimal points and store it in a binary form, you are likely to get errors for some inputs.

quant_dev
  • 6,181
  • 1
  • 34
  • 57