How define decimal places in a float in c++

Hi, how define decimal places in c++, because in C i know, for example:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

int main()
{
float cuzin;

cuzin = 5;

printf("%5.3f %20.3f\n", cuzin, cuzin); //In C

cout << cuzin << " " << cuzin << endl; //C++?????

return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cstdio>
#include <iostream>
#include <iomanip>

int main()
{
   float cuzin = 5 ;

   std::printf( "%5.3f %20.3f\n", cuzin, cuzin );

   std::cout << std::fixed << std::setprecision(3)
             << std::setw(5) << cuzin << ' ' << std::setw(20) << cuzin << '\n' ;
}


If the C++ equivalent looks needlessly verbose (after all, lounge kiddies do complain bitterly - and repeatedly - about how poorly the C++ input/output library is designed), consider this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <cstdint>
#include <cinttypes>

int main()
{
   float cuzin = 5 ;
   int precision ;
   std::uint32_t value ;

   {
       std::scanf( "%d", &precision ) ;

       char format[128] ;
       std::sprintf( format, "%%5.%df %%20.%df\n", precision, precision ) ;

       std::printf( format, cuzin, cuzin );

       std::scanf( "%" SCNu32, &value ) ;
       std::printf( "%" PRIu32 "\n", value ) ;
   }

   {
       std::cin >> precision ;
       std::cout << std::fixed << std::setprecision(precision)
             << std::setw(5) << cuzin << ' ' << std::setw(20) << cuzin << '\n' ;

       std::cin >> value ;
       std::cout << value << '\n' ;
   }
}

What would happen if we want to change the type of the variable from std::uint32_t to std::uint_fast32_t?
Topic archived. No new replies allowed.