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
🌐
Quora
quora.com › How-do-you-convert-double-to-int-in-C
How to convert double to int in C - Quora
Well, it’s closer to that 3 than to any other number with 6 decimal digits—but it’s still below 3, so it still truncates to 2. The standard library has functions you can call to trunc toward 0 (as above), floor toward the smaller integer (so -2.9 becomes -3), or ceil toward the larger integer (so 2.9 becomes 3), and a family of round / nearbyint / rint functions to round toward the nearest integer (handling cases exactly halfway differently).
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 - how to truncate a number with a decimal point into a int? what's the function for this? - Stack Overflow
The problem occurs when I do a division operation. I would like to know who to truncate a number with a decimal point into a whole number such as 2, 4, 67. More on stackoverflow.com
🌐 stackoverflow.com
c++ - Truncating double without rounding in C - Stack Overflow
Can you multiply by 100 and then truncate to an integer? Then you could format the result like one would with dollars and cents. Simply dividing by 100 might land you back at square one due to floating-point representation issues. ... Not really. Standard double floating point representations ... More on stackoverflow.com
🌐 stackoverflow.com
How can I safely truncate a signed integer?
Couldn't you just test that the number is within the proper bounds INT_MAX and INT_MIN and then use normal assignment? More on reddit.com
🌐 r/C_Programming
25
1
February 27, 2022
double to int conversion - C++ Forum
I didn't think so :) The main point is that converting double to int is truncated unless you use a function to do it differently, as best as I see the question (which is not well worded, so I could be wrong). The rest of it is examples of how using the default truncation is probably a bad idea ... More on cplusplus.com
🌐 cplusplus.com
June 19, 2018
🌐
Cplusplus
cplusplus.com › reference › cmath › trunc
Cplusplus
Additional overloads are provided in this header (<cmath>) for the integral types: These overloads effectively cast x to a double before calculations (defined for T being any integral type). ... Value to truncate.
🌐
Medium
medium.com › @mayintuji › truncate-numbers-in-c-6c0d6cf4eff6
Truncate Numbers in C | by Mayin Dev | Medium
April 11, 2025 - This involves removing the fractional part of a floating-point number, leaving only the integer part. A simple way to achieve this in C is using type casting. For instance, casting a double to an int will truncate the fractional part.
🌐
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 - #include <iostream> int main() { double pi = 3.14159; int rounded = (int)pi; std::cout << "Original: " << pi << ", Rounded: " << rounded << std::endl; return 0; } ... This method simply truncates the decimal part, always rounding towards zero.
🌐
Fresh2Refresh
fresh2refresh.com › home › c programming tutorial › c – arithmetic functions › c – trunc() function
C trunc() function | C Arithmetic functions | Fresh2Refresh
September 23, 2020 - trunc( ) function in C truncates the decimal value from floating point value and returns integer value. ”math.h” header file supports trunc( ) function in C language. Syntax for trunc( ) function in C is given below. double trunc (double a); float truncf (float a); long double truncl (long ...
🌐
codestudy
codestudy.net › blog › how-to-convert-double-to-int-in-c
How to Convert double to int in C: Fix Incorrect Truncation (3669.0 → 3668) and Truncate Properly (3559.8 → 3559)
We’ll explain why "incorrect" ... in your code. ... C’s default casting behavior for double → int is to truncate towards zero....
Find elsewhere
🌐
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 ...
🌐
Reddit
reddit.com › r/c_programming › how can i safely truncate a signed integer?
r/C_Programming on Reddit: How can I safely truncate a signed integer?
February 27, 2022 -

How can I safely convert a long to int by discarding all bits beyond 33? If I simply cast, if the value in the long cannot be represented in int (i.e. overflows), the behavior is undefined. How can I get around this? I could cast them to unsigned prior to truncation, but then how can I safely convert unsigned int to int without invoking undefined behavior if the MSB is set? Is the following undefined behavior:

(int)(unsigned int)(int){-1};
🌐
W3Schools
w3schools.com › c › ref_math_trunc.php
C Math trunc() Function
C Examples C Real-Life Examples ... trunc(-5.9)); Try it Yourself » · The trunc() function truncates a number, which means returning only the integer part of the number....
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › reference › trunc-truncf-truncl
trunc, truncf, truncl | Microsoft Learn
July 9, 2025 - If successful, the functions return an integer value of x, rounded towards zero. Otherwise, the functions may return one of the following values: Errors are reported as specified in _matherr. Because C++ allows overloading, you can call overloads of trunc that take and return float and long double types.
🌐
TutorialsPoint
tutorialspoint.com › trunc-truncf-truncl-in-c-language
trunc() , truncf() , truncl() in C language
#include <stdio.h> #include <math.h> main() { double a, b, x, y; x = 53.26; y = 75.86; a = trunc(x); b = trunc(y); printf("The value of a: %lf ",a); printf("The value of a: %lf ",b); } The value of a: 53.000000 The value of a: 75.000000 · This function is used to truncate floating type value. And return only the integer part.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › trunc-truncf-truncl-c-language
trunc() , truncf() , truncl() in C language - GeeksforGeeks
February 6, 2023 - Syntax : double trunc(double x); Parameters: x :It takes a double value as an input and then truncates the values after the decimal point. Return Value : It returns a double value whose values after the decimal point is 0 only.
🌐
Cprogramming
cboard.cprogramming.com › cplusplus-programming › 48110-truncating-double.html
Truncating a double? - C Board - Cprogramming.com
December 8, 2003 - ... double D; ... //something goes into D D=static_cast<int>(D*100)/100.0; ... that sets 12.345678 to 1234.567890 and then casts it as an int which would truncate it to 1234, then divides it by 100.0 to make it 12.3400000. use 100.0 instead of 100 to ensure that it doesn't truncate it again to 12.
🌐
Cplusplus
cplusplus.com › forum › general › 238465
double to int conversion - C++ Forum
June 19, 2018 - 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 back, as 5 can be converted to 4.999999999999999999999999999999 and then back to 4 (!!!). So the default truncate is not usually what you want, you usually want to invoke an extra routine like round, floor, ceil, etc.