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
... Converting a double to an int in C requires choosing how you want to handle the fractional part (truncate, round, clamp, handle overflow) and then using the appropriate cast or function.
Discussions

types - How to convert int to Double in C programming - Stack Overflow
int num1, num2; double average; average=(double)(num1+num2)/2; printf("average: %d", average); My test printf shows average as: 0 This is maybe too easy, but I can't see it. My inputs are both "... More on stackoverflow.com
🌐 stackoverflow.com
Faster way to convert double to string, not using "%f"?
You mean something like ftoa() and itoa(). Basically under the hood, they do the same thing in the same way by using division and modulo as the functions you mentioned. More on reddit.com
🌐 r/C_Programming
22
1
October 16, 2022
double to int conversion - C++ Forum
What in the world are you talking about? What is the point of this? BTW, intelligent people post complete programs. ... double to int is really simple. It drops the decimal. 3.14 is 3 and so is 3.99999999 this can be a problem if you have integer values stored (or converted to) doubles and ... More on cplusplus.com
🌐 cplusplus.com
June 19, 2018
General C: How to convert a Double to Int and back for ...
Loading · ×Sorry to interrupt · Refresh More on forum.microchip.com
🌐 forum.microchip.com
September 16, 2016
🌐
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...
🌐
Cppreference
en.cppreference.com › w › c › language › conversion.html
Implicit conversions - cppreference.com
March 31, 2025 - 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.
🌐
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"?
October 16, 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.

Find elsewhere
🌐
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 ...
🌐
Cplusplus
cplusplus.com › forum › general › 238465
double to int conversion - C++ Forum
June 19, 2018 - 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.
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_type_casting.htm
Type Casting in C
Here, it is simple to understand that first c gets converted to an integer, but as the final value is double, usual arithmetic conversion applies and the compiler converts i and c into 'float' and adds them yielding a 'float' result.
🌐
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; }
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 180437-very-weird-issue-double-int-conversion-inside-loop.html
Very weird issue with double to int conversion inside the loop
July 8, 2021 - Have a read of: The Floating-Point Guide - What Every Programmer Should Know About Floating-Point Arithmetic The issue is that the library is calculating pow(10,2) as something just slightly under 100.0 and it is being truncated down to 99 when it is converted to an integer. ... This is also my suspicion... Since x^y (^ as potency) is calculated as e^(y*ln(x)) and, maybe, MinGW implementation of pow() can be a little bit wrong (there's a final approximation that need to be made), one or more of 10^x can be approximated, not exact... In the latest glibc (linux) the results are correct, but I don't know if mingw libc (libm) is up to date, hence my question about the F and E fields of the double value calculated by pow(10,i), that should be: For 10^0: F=0, E=0 For 10^1: F=1125899906842624, E=3 For 10^2: F=2533274790395904, E=6 In the equation (since these values are normalized):
🌐
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?
🌐
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 › article › cplusplus-program-to-convert-double-type-variables-into-int
C++ Program to convert double type Variables into int
October 19, 2022 - There is a problem of data loss associated in this conversion, as integer variables cannot contain the fractional values after the decimal point. double input = <double value>; int output = input;
🌐
Cplusplus
cplusplus.com › forum › beginner › 198640
Data type conversion int to double withi - C++ Forum
September 29, 2016 - Both a and b are int, so it is clear that the compiler uses #1 for a / b. If you do cast both a and b to double, then the use of #2 is obvious. What if only one side is double, as in static_cast<double>(a) / b? The compiler has two options: 1. Implicitly convert b to double and then call #2 2.