https://en.cppreference.com/w/cpp/language/explicit_cast Basically, it is equivalent to a C-style cast (int) d, which does consider static_cast, but could also do a reinterpret_cast if valid. Answer from marko312 on reddit.com
🌐
Cplusplus
cplusplus.com › forum › beginner › 130827
static casting to double - C++ Forum
double Average(num1, num2); void ... comes out as: 5.00 and it should be 5.57 or 5.58. ... You have to cast in the expression: lastYearAvg = static_cast<double>(num1)/num2; Also, your Average function returns a garbage value (it returns the uninitialized classAverage variable ...
🌐
Learn C++
learncpp.com › cpp-tutorial › introduction-to-type-conversion-and-static_cast
4.12 — Introduction to type conversion and static_cast – Learn C++
October 21, 2021 - In the above example, the conversion does not change variable y from type int to double or the value of y from 5 to 5.0. Instead, the conversion uses the value of y (5) as input, and returns a temporary object of type double with value 5.0. This temporary object is then passed to function print. ... Some advanced type conversions (e.g. those involving const_cast or reinterpret_cast) do not return temporary objects, but instead reinterpret the type of an existing value or object.
Discussions

Is `static_cast<X>(y)` the same as `X(y)` for simple data types like `int` and `double`?
https://en.cppreference.com/w/cpp/language/explicit_cast Basically, it is equivalent to a C-style cast (int) d, which does consider static_cast, but could also do a reinterpret_cast if valid. More on reddit.com
🌐 r/cpp_questions
18
16
August 11, 2020
casting - In C++, what are the differences between static_cast (a) and double(a)? - Stack Overflow
Have you ever seen the latter? It seems to me I saw it in some snippet written by Stroustrup but I can't find the reference. ... When a is an int, static_cast(a), (double)a, and double(a) are all semantically identical. More on stackoverflow.com
🌐 stackoverflow.com
Rounding with static_cast<int>? - c++
I feel really silly asking this because I know how to do it 101 ways, but not the way it is defined in the book. (note, I know C++) So far, we have only gone over the very basics of C++. So basica... More on stackoverflow.com
🌐 stackoverflow.com
October 14, 2019
Number Type Cast
Hi, I often need to convert/cast integer variables to double for multiplication/division: int a = 10; int b = 3; double c = static_cast(a) / b; In general ... More on forum.qt.io
🌐 forum.qt.io
20
0
March 1, 2025
🌐
Cppreference
en.cppreference.com › w › cpp › language › static_cast.html
static_cast conversion - cppreference.com
August 18, 2024 - union U { int a; double b; } u; void* x = &u; // x's value is “pointer to u” double* y = static_cast<double*>(x); // y's value is “pointer to u.b” char* z = static_cast<char*>(x); // z's value is “pointer to u” ·
🌐
Reddit
reddit.com › r/cpp_questions › is `static_cast(y)` the same as `x(y)` for simple data types like `int` and `double`?
r/cpp_questions on Reddit: Is `static_cast<X>(y)` the same as `X(y)` for simple data types like `int` and `double`?
August 11, 2020 -

Say I had the lines:

double d = 1.1;
auto a = int(d);
auto b = static_cast<int>(d);

I understand that I'm doing a cast of a double to an in for the creation of variable b, but is that also happening as well for the creation of a? If I can use is as a shorthand for doing static casts, I'd love to. But if that constructor style variable creation isn't actually doing a static cast, I'd like to know too.

EDIT:

I would say this is my favourite answer of the bunch: https://www.reddit.com/r/cpp_questions/comments/i76d6e/is_static_castxy_the_same_as_xy_for_simple_data/g119fnu/

🌐
Learn C++
learncpp.com › cpp-tutorial › explicit-type-conversion-casting-and-static-cast
10.6 — Explicit type conversion (casting) and static_cast – Learn C++
July 28, 2025 - static_cast<double>(x) returns a temporary double object containing the converted value 10.0. This temporary is then used as the left-operand of the floating point division. There are two important properties of static_cast. First, static_cast provides compile-time type checking.
Find elsewhere
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › cpp › static-cast-operator
static_cast Operator | Microsoft Learn
April 3, 2025 - The static_cast operator can also be used to perform any implicit conversion, including standard conversions and user-defined conversions. For example: // static_cast_Operator_3.cpp // compile with: /LD /GR typedef unsigned char BYTE; void f() { char ch; int i = 65; float f = 2.5; double dbl; ch = static_cast<char>(i); // int to char dbl = static_cast<double>(f); // float to double i = static_cast<BYTE>(ch); }
🌐
Learn C++
learncpp.com › cpp-tutorial › numeric-conversions
10.3 — Numeric conversions – Learn C++
September 29, 2016 - int main() { int n { 5 }; long l = n; // okay, produces long value 5 short s { 5 }; double d = s; // okay, produces double value 5.0 return 0; } Compilers will typically not issue warnings for implicit value-preserving conversions. A value converted using a value-preserving conversion can always be converted back to the source type, resulting in a value that is equivalent to the original value: #include <iostream> int main() { int n = static_cast<int>(static_cast<long>(3)); // convert int 3 to long and back std::cout << n << '\n'; // prints 3 char c = static_cast<char>(static_cast<double>('c')); // convert 'c' to double and back std::cout << c << '\n'; // prints 'c' return 0; }
🌐
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.
🌐
FavTutor
favtutor.com › blogs › static-cast-cpp
C++ static_cast Operator Explained (with Examples)
October 19, 2022 - In this example, we have an integer variable `myInt` which we want to convert to a double. By using static_cast, we explicitly specify the desired type conversion. The result is a new double variable `myDouble` that holds the converted value.
🌐
Developer Community
developercommunity.visualstudio.com › t › using-static-cast-to-cast-a-large-negative-double › 519454
Using static_cast to cast a large negative double value ...
Skip to main content · Microsoft · Visual Studio · Sign in · You need to enable JavaScript to run this app · Sorry this browser is no longer supported · Please use any other modern browser like 'Microsoft Edge'
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 128202 › issues-with-static-cast-double
issues with static_cast< double > - c++
February 22, 2011 - Also check the factorial routine for the 0! case and confirm all variables (like numerator) are initialized to the expected values. term = static_cast< double >((numerator/denominator));
Top answer
1 of 2
5

I can not get a simple conversion from int to double to work

Actually, the conversion itself works just fine.

d1 and d2 equals 8, instead of 8.0

8 and 8.0 are two ways of representing the same value as a string. There is no difference in the value.

AFAIK, d1 and d2 should be 8.0, shouldn't then?

They are, because 8 and 8.0 are the same exact value.

Can anyone, please, tell me what am I doing wrong?

Your mistake is expecting the output to be 8.0. The correct output is 8, because of the default formatting settings of the output stream.

Another mistake is assuming that there is a problem with the conversion from int to double, when the problem is actually in the formatting of the textual output.

If your intention was to output 8.0, then your mistake was to not use the correct stream manipulators to achieve that format. In particular, you need to use the std::fixed manipulator to show fixed number of decimals, even when they are zero. You can use std::setprecision to set the number of decimals to show. Depending on how you want the output formatted in different cases, std::showpoint might also be an option.

2 of 2
4

Make no mistake, d1 and d2 are doubles. You need std::setprecision() (and apparently std::fixed) too found in the header <iomanip>:

#include <iostream>
#include <iomanip>

int main()
{
  int i = 8;
  double d1 = i;
  double d2 = static_cast<double>(i);
  std::cout << "i = " << i << std::fixed << std::setprecision(2) << ", d1 = " << d1 << ", d2 = " << d2 << std::endl;
  return 0;
}
🌐
TutorialsPoint
tutorialspoint.com › article › cplusplus-program-to-convert-int-variables-into-double
C++ Program to convert int Variables into double
October 19, 2022 - #include <iostream> using namespace std; double solve(int value) { double opVal = static_cast<double>(value); return opVal; } int main() { int ip = 55; double op = solve(ip); cout<< "The input value is: " << ip << endl; cout<< "The output value is: " << op << endl; return 0; }