printf("%011.05lf", 111.111); in C++
-
Hallo, wie würde man das:
printf("%011.05lf", 111.111);
in C++ schreiben?
Das:
srand(time(0)); printf("%05d", rand()%10000+1);
schreibt man ja in C++ so:
srand(time(0)); cout<<setw(5)<<setfill('0')<<rand()%10000+1;
oder?
Ist nur reines Interesse.
-
|M| schrieb:
Hallo, wie würde man das:
printf("%011.05lf", 111.111);
in C++ schreiben?
cout<<setfill('0')<<setw(11)<<setprecision(5)<<fixed<<111.111; // %0 11 .05 lf
Das:
srand(time(0)); printf("%05d", rand()%10000+1);
schreibt man ja in C++ so:
srand(time(0)); cout<<setw(5)<<setfill('0')<<rand()%10000+1;
oder?
Ja, das kommt hin.
(PS: Ganz exakt kannst du die printf()-Formate nicht auf IOStreams übersetzen)
-
oder sowas
#include <iostream> #include <string> #include <stdarg.h> std::string str_printf (char* f, ... ) { char s[1000]; va_list vl; va_start (vl, f); vsprintf (s, f, vl); va_end (vl); return s; } // test int main() { std::cout << str_printf ("%011.05lf", 111.111) << std::endl; }