I suspect you don't actually have that problem - I suspect you've really got:

double a = callSomeFunction();
// Examine a in the debugger or via logging, and decide it's 3669.0

// Now cast
int b = (int) a;
// Now a is 3668

What makes me say that is that although it's true that many decimal values cannot be stored exactly in float or double, that doesn't hold for integers of this kind of magnitude. They can very easily be exactly represented in binary floating point form. (Very large integers can't always be exactly represented, but we're not dealing with a very large integer here.)

I strongly suspect that your double value is actually slightly less than 3669.0, but it's being displayed to you as 3669.0 by whatever diagnostic device you're using. The conversion to an integer value just performs truncation, not rounding - hence the issue.

Assuming your double type is an IEEE-754 64-bit type, the largest value which is less than 3669.0 is exactly

3668.99999999999954525264911353588104248046875

So if you're using any diagnostic approach where that value would be shown as 3669.0, then it's quite possible (probable, I'd say) that this is what's happening.

Answer from Jon Skeet on Stack Overflow
Top answer
1 of 5
74

I suspect you don't actually have that problem - I suspect you've really got:

double a = callSomeFunction();
// Examine a in the debugger or via logging, and decide it's 3669.0

// Now cast
int b = (int) a;
// Now a is 3668

What makes me say that is that although it's true that many decimal values cannot be stored exactly in float or double, that doesn't hold for integers of this kind of magnitude. They can very easily be exactly represented in binary floating point form. (Very large integers can't always be exactly represented, but we're not dealing with a very large integer here.)

I strongly suspect that your double value is actually slightly less than 3669.0, but it's being displayed to you as 3669.0 by whatever diagnostic device you're using. The conversion to an integer value just performs truncation, not rounding - hence the issue.

Assuming your double type is an IEEE-754 64-bit type, the largest value which is less than 3669.0 is exactly

3668.99999999999954525264911353588104248046875

So if you're using any diagnostic approach where that value would be shown as 3669.0, then it's quite possible (probable, I'd say) that this is what's happening.

2 of 5
7
main() {
    double a;
    a=3669.0;
    int b;
    b=a;
    printf("b is %d",b);
  
}

output is :b is 3669

when you write b=a; then its automatically converted in int

see on-line compiler result : 

http://ideone.com/60T5b


This is called Implicit Type Conversion Read more here https://www.geeksforgeeks.org/implicit-type-conversion-in-c-with-examples/

🌐
Quora
quora.com › How-do-you-convert-double-to-int-in-C
How to convert double to int in C - Quora
Answer (1 of 5): Keep in mind that it’s not safe to do this in general. If you don’t care… well, you should, but if you don’t… For the most part (with varargs functions like [code ]printf[/code] being a notable exception), you can just use a [code ]double[/code] where an [code ]int[/code] ...
🌐
Medium
medium.com › @ryan_forrester_ › double-to-integer-conversion-in-c-practical-guide-49360f4ebf60
Double to Integer Conversion in C++: Practical Guide | by ryan | Medium
October 4, 2024 - Let’s dive into the various methods, their implications, and how to choose the right approach for your specific needs. The simplest way to convert a double to an integer in C++ is through type casting.
🌐
Quora
quora.com › How-do-I-convert-double-to-int-in-C-simply-by-a-function-and-not-by-type-casting
How to convert double to int in C simply by a function and not by type casting - Quora
Answer (1 of 4): That's an out of the box question. I am glad you asked it. If you don't want to type cast ta double value for an int and just want to use a function for that than there are two such functions to fulfill your requirement. 1. ceil(double x): which rounds the value x of double data...
🌐
Cplusplus
cplusplus.com › forum › general › 238465
double to int conversion - C++ Forum
So the default truncate is not usually what you want, you usually want to invoke an extra routine like round, floor, ceil, etc. ... No it can't. As long as the int doesn't have too many digits to fit into the precision of the double it won't be changed at all.
🌐
Cprogramming
cboard.cprogramming.com › cplusplus-programming › 64313-converting-double-int.html
Converting from double to int
double b = 2.5764; int c = 0; c = (int)b; //the int tells the comiler that you MEANT to convert from a double to and int //and the compiler won't warn you that you are losing some precision in the conversion // c == 2 == true // b == 2.5764 == true; edit: actually "(int) b" means evaluate 'b' ...
Find elsewhere
🌐
Reddit
reddit.com › r/c_programming › faster way to convert double to string, not using "%f"?
r/C_Programming on Reddit: Faster way to convert double to string, not using "%f"?
August 28, 2022 -

Have to output millions of doubles to text files, so wondering if there is a faster way to convert double => text, besides using %f in one of many settings printf, sprintf, others I could probably find.

In the way that you could convert an int to text by using modulo and division to get the decimal digits. I assume that's faster than running through %d.

EDIT: I finally settled on %d and multiplying the double values by 1000000, converting to int, then converting back on the receiving end. More than enough precision for my specific application, not really elegant, but the time saving is significant. The double->int and reverse conversions are just a few instructions, much faster than converting the double to a string and back to double.

🌐
Quora
quora.com › In-C-can-you-type-cast-a-double-into-an-int-How-is-it-treated
In C, can you type cast a double into an int? How is it treated? - Quora
Are you just trying to work with the value of your double, and truncate it into an int? If so, you can write “int i =(int)myDouble” But this will remove the decimal part of your integer. On the other hand, if you are trying to store the ...
🌐
Runestone Academy
runestone.academy › ns › books › published › thinkcpp › Chapter3 › ConvertingFromDoubleToInt.html
3.2. Converting from double to int — How to Think Like a Computer Scientist - C++
The int function returns an integer, so x gets the value 3. Converting to an integer always rounds down, even if the fraction part is 0.99999999. For every type in C++, there is a corresponding function that typecasts its argument to the appropriate type. Q-1: In the lab, we measured a temperature of 7.99999999 degrees C, using an extremely precise measuring device. Now we are writing a program to perform some calculations with our data. Consider the following C++ code. int main () { double temp = 7.99999999; int roundedTemp = int (temp); cout << roundedTemp; }
🌐
The Coding Forums
thecodingforums.com › archive › archive › c++
converting double to int | C++ | Coding Forums
November 19, 2013 - #include <iostream> #include <ostream> int main() { ::std::cout << int( 2.6 )<< '\n'; ::std::cout << int{ 2.6 }<< '\n'; ::std::cout << ( int )2.6 << '\n'; ::std::cout << static_cast< int >( 2.6 )<< '\n'; } Under which circumstances is which way of the above to convert a double to an int the best style?
🌐
Cppreference
en.cppreference.com › w › c › language › conversion.html
Implicit conversions - cppreference.com
If control over FE_INEXACT is needed in floating-to-integer conversions, rint and nearbyint may be used. double d = 10; // d = 10.00 float f = 20000001; // f = 20000000.00 (FE_INEXACT) float x = 1 + (long long)FLT_MAX; // undefined behavior · A value of any real floating type can be implicitly converted to any other real floating type.
🌐
TutorialsPoint
tutorialspoint.com › home › cprogramming › c type casting
C Type Casting
June 10, 2012 - If any of the operands is double − The result is promoted to double. Integer promotion is the process by which values of integer type "smaller" than int or unsigned int are converted either to int or unsigned int. Consider an example of adding a character with an integer − · #include <stdio.h> int main() { int i = 17; char c = 'c'; /* ascii value is 99 */ int sum; sum = i + c; printf("Value of sum : %d\n", sum); }
🌐
LinuxQuestions.org
linuxquestions.org › questions › programming-9 › how-can-i-cast-an-int-to-a-double-c-435814
How can I cast an int to a double (C)?
How can I cast an int, stored in a variable, to a double? I am trying to do this: Code: double d; d = (double)strlen(str) \ (double)cols Its obviously
🌐
TutorialsPoint
tutorialspoint.com › cplusplus-program-to-convert-double-type-variables-into-int
C++ Program to convert double type Variables into int
The compiler will take care of ... conversion, as integer variables cannot contain the fractional values after the decimal point. double input = <double value>; int output = input;...