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/

Discussions

c - Convert int to double - Stack Overflow
I ran this simple program, but when I convert from int to double, the result is zero. The sqrt of the zeros then displays negative values. This is an example from an online tutorial so I'm not su... More on stackoverflow.com
🌐 stackoverflow.com
How do I convert an int directly to a double?
From your error message I wonder, how are you using the doubleValue() function? it should be used var.doubleValue() not doubleValue(var), thought it would be good to double check More on reddit.com
🌐 r/processing
8
4
September 15, 2019
Data type conversion int to double withi - C++ Forum
I'm in my first programming class and am having a little trouble understanding what I should do. I extracted the trouble shooting problem from my actual assignment, but it still does not make sense to me/ I don't know how to get around it. #include using namespace std; int main() { int a = 5; int b = 2; double ... More on cplusplus.com
🌐 cplusplus.com
September 29, 2016
[C++] How do you know when to use an 'int' or 'double', what is common practice?
The whole thing about choosing these types is knowing the limits. It is efficiency - if you have a number that you know will always be in a certain range, choosing the smallest possible version of it will be more efficient as the CPU will be able to do arithmetic faster. The counterpoint is that choosing the wrong limits will cause bugs. More on reddit.com
🌐 r/learnprogramming
7
2
December 26, 2020
🌐
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
🌐
Convertdatatypes
convertdatatypes.com › Convert-int-to-double-in-ObjectiveC.html
Convert int to double in C | Convert Data Types
Convert int to double in C. ConvertDataTypes is the helpfull website for converting your data types in several programming languages.
🌐
Cppreference
en.cppreference.com › w › c › language › conversion.html
Implicit conversions - cppreference.com
March 31, 2025 - double d = 0.1; // d = 0.1000000000000000055511151231257827021181583404541015625 float f = d; // f = 0.100000001490116119384765625 float x = 2 * (double)FLT_MAX; // undefined · A pointer to void can be implicitly converted to and from any pointer to object type with the following semantics: If a pointer to object is converted to a pointer to void and back, its value compares equal to the original pointer. No other guarantees are offered. int* p = malloc(10 * sizeof(int)); // malloc returns void*
🌐
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++
As I mentioned, C++ converts int s to double s automatically if necessary, because no information is lost in the translation. On the other hand, going from a double to an int requires rounding off.
Find elsewhere
🌐
Quora
quora.com › How-do-you-convert-double-to-int-in-C
How to convert double to int in C - Quora
This is all found in the “Nearest integer functions” section of the standard (e.g., 7.12.9 in C17). You can do any of these to the double, and then convert the result (which now won’t have any non-integral part to worry about, because you already took care of it) to an int.
🌐
W3Schools
w3schools.com › c › c_type_conversion.php
C Data Type Conversion
It is important that you know how the compiler work in these situations, to avoid unexpected results. As another example, if you divide two integers: 5 by 2, you know that the sum is 2.5. And as you know from the beginning of this page, if you store the sum as an integer, the result will only display the number 2. Therefore, it would be better to store the sum as a float or a double...
🌐
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.
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_type_casting.htm
Type Casting in C
While performing the implicit or automatic type conversions, the C compiler follows the rules of type promotions. Generally, the principle followed is as follows − · Byte and short values − They are promoted to int. If one operand is a long − The entire expression is promoted to long. If one operand is a float − The entire expression is promoted to float. If any of the operands is double − The result is promoted to double.
🌐
IQCode
iqcode.com › code › c › int-to-double-c
int to double c Code Example
#include <stdio.h> main() { int sum = 17, count = 5; double mean; mean = (double) sum / count; printf("Value of mean : %f\n", mean ); } ... C 2022-03-27 22:30:45 Problem Statement Print the following output: \ Input Format IN Output Format \ C 2022-03-27 21:35:04 Write a c code to create a data base of students using structure.
🌐
Reddit
reddit.com › r/learnprogramming › [c++] how do you know when to use an 'int' or 'double', what is common practice?
r/learnprogramming on Reddit: [C++] How do you know when to use an 'int' or 'double', what is common practice?
December 26, 2020 -

So let's say we have two variables that are both 'int', if we want to divide these numbers then we will get a number that is rounded off to the nearest zero if the divison doesn't equal to a whole number.

//skip eplanation that I am not sure what I am explaining

Why not just use double for everything if it is more accurate in getting the correct answer. If you divide two int's that don't equal a whole number it will be rounded. So why not save all the hassle and just use 'double' throughout 5 to 20 line programs?

Excuse noob question from noob :)

🌐
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
To convert a double to an int without using an explicit cast in the call site, write a small converter function that performs the conversion logic and returns an int. Several behaviors are possible (truncate toward zero, floor, round, clamp to range).
🌐
MyCleverAI
mycleverai.com › it-questions › how-do-i-cast-an-int-to-a-double-in-c
How do I cast an int to a double in C?
In C, casting an int to a double is straightforward and can be done using a type cast.
🌐
TutorialsPoint
tutorialspoint.com › article › cplusplus-program-to-convert-int-variables-into-double
C++ Program to convert int Variables into double
October 19, 2022 - One is the C-styled version and another one is the function-styled casting. We mention the resultant datatype before the source variable or the value enclosed within brackets. int input = <integer value>; double output = (double) input;
🌐
Reddit
reddit.com › r/c_programming › what is the difference between int and double and how do you know when to use them?
r/C_Programming on Reddit: What is the difference between int and double and how do you know when to use them?
February 6, 2015 - Well they can't really be interchanged. An int is used to represent integer numbers (+- whole numbers). While a double is used to represent a double precision floating point number.
🌐
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
A double is 8 bytes, so it cannot be stored safely in an int, as it will depend on wether the compilation happens in 32 or 64 bits. To be on the safe side, it is possible to use a “long long”, guaranteed to be at least 64 bits.