dimanche 27 septembre 2015

Why is (long) always rounding the number down?

yesterday I faced a problem that I could not explain. I was writing a very simple function that converts a double number to long. It was a part of another program. I used the following code:

long converter(double x) {
return (long) x; 
}

It worked perfectly until I entered 1.9 as the input and the result was 1. But 1.9 is closer to 2 than it is to 1. So, the result should be 2 rather than 1. Why is this problem happening? Can you give me a solution that solves this irritating problem?

Aucun commentaire:

Enregistrer un commentaire