-1

Example of values I am getting

 12.230030
 4.000000
 400.402100
 132.000000
 53.120203
 100.0010
 45.320030

I want this floating values to print like this

12.230030
4
400.4021
132
53.120203
100.001
45.32003
Ankit Mishra
  • 530
  • 8
  • 16

1 Answers1

0

Simply use printf function.

float val=12.02300;
printf("%.8g",val);

Will give correct output. In this case it will output as 12.023
example of other output For value=400.00; it will output 400
For value=32.122300 it will output 32.1223 .

Ankit Mishra
  • 530
  • 8
  • 16
  • 1
    Was this a C or a C++ question? We do not use ````printf```` in C++. C++ uses ````iostreams````. The following will do: #include #include int main() { std::vector values{ 12.230030, 4.000000, 400.402100, 132.000000, 53.120203, 100.0010, 45.320030 }; for (const double d : values) std::cout << d << "\n"; return 0; } – A M Apr 26 '20 at 06:53