Convert from Double to Integer w/o Rounding in C++
-
Hi, I want to be able to convert a double to an integer. Say the double variable is a decimal, like 1.732. If I use int() to convert it, I will get 2. I just want the whole number of the double variable, 1, without any rounding. How do I do this in C++?
-
Hi, I want to be able to convert a double to an integer. Say the double variable is a decimal, like 1.732. If I use int() to convert it, I will get 2. I just want the whole number of the double variable, 1, without any rounding. How do I do this in C++?
I would try a simple cast. Warning: whatever you end up with, make sure it works the way you want it for negative numbers too! :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
I would try a simple cast. Warning: whatever you end up with, make sure it works the way you want it for negative numbers too! :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
Don't typecasts round the decimal to a whole number when going from double to integer? And isn't int() a typecast? BTW thanks for the help...
did you try anything? did you look it up in your C++ book? did you google it? see, asking questions is the easy part. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
Hi, I want to be able to convert a double to an integer. Say the double variable is a decimal, like 1.732. If I use int() to convert it, I will get 2. I just want the whole number of the double variable, 1, without any rounding. How do I do this in C++?
Casting and assigning to an integer will not do any rounding. The integer will only store what it can accommodate, which is the integer part of the decimal number.
«_Superman_» _I love work. It gives me something to do between weekends.
-
Hi, I want to be able to convert a double to an integer. Say the double variable is a decimal, like 1.732. If I use int() to convert it, I will get 2. I just want the whole number of the double variable, 1, without any rounding. How do I do this in C++?
What about using floor or ceil before casting to int? Actually, the return value of those function is double again, but I do not know how bad the consequences could be - maybe round after those functions...
-
Hi, I want to be able to convert a double to an integer. Say the double variable is a decimal, like 1.732. If I use int() to convert it, I will get 2. I just want the whole number of the double variable, 1, without any rounding. How do I do this in C++?
I thought it was a standard thing, but when I do
int(1.732)
I get 1 andint(-1.732)
I get -1. A look at the disassembly shows that a function from the CRT (_ftol2
in my case) is used, so it could be implementation specific. If this is the case then you should use something like floor/ceil as Bernhard Hiller suggested. This function will result in the behaviour described above, where numbers round towards 0. It includes epsilon rounding to account for epsilon errors. In some cases 3 = 2.99999999999 (or something similar) due to rounding errors in previous operations. See http://en.wikipedia.org/wiki/Double_precision[^] for more detailsint DoubleToInt(double nDouble) {
if (nDouble >= 0) {
retrun (int)floor(nDouble + DBL_EPSILON);
} else {
retrun (int)ceil(nDouble - DBL_EPSILON);
}
} -
I thought it was a standard thing, but when I do
int(1.732)
I get 1 andint(-1.732)
I get -1. A look at the disassembly shows that a function from the CRT (_ftol2
in my case) is used, so it could be implementation specific. If this is the case then you should use something like floor/ceil as Bernhard Hiller suggested. This function will result in the behaviour described above, where numbers round towards 0. It includes epsilon rounding to account for epsilon errors. In some cases 3 = 2.99999999999 (or something similar) due to rounding errors in previous operations. See http://en.wikipedia.org/wiki/Double_precision[^] for more detailsint DoubleToInt(double nDouble) {
if (nDouble >= 0) {
retrun (int)floor(nDouble + DBL_EPSILON);
} else {
retrun (int)ceil(nDouble - DBL_EPSILON);
}
}Thanks. I thought I read somewhere that int() rounded down when converting from double, though it didn't seem so when I tested it. I think the whole thing that brought up this issue was the fact that int() doesn't handle decimals like 1.99999... correctly. Therefore it was throwing off everything when it rounded 1.999... to 2. The above function works perfectly, so long as you make sure to include both float.h and cmath. Thanks again!