In a C++ environment, if you just output a double variable with cout, the desired function will be performed. The default setting of cout determines and outputs the decimal place of the real number.
#include <iostream>
using namespace std;
int main()
{
double x = 1;
cout << x << '\n';
x = 1.2;
cout << x << '\n';
}
There is no function in C that does it automatically, but it has to be implemented directly.
#include <stdio.h>
int main()
{
double x = 1;
if (x == (double)((int)x))
printf("%d", (int)x);
else
printf("%lf", x);
printf("\n");
x = 1.2;
if (x == (double)((int)x))
printf("%d", (int)x);
else
printf("%lf", x);
return 0;
}
© 2024 OneMinuteCode. All rights reserved.