double format

Started by
4 comments, last by Austrian Coder 20 years, 10 months ago
Hi! I have double b = 0.1; is there a function, which adds me an 0 to b? b = 0.10 I know it sounds silly. But i have an simple math programm (learning simple math stuff with computer). And I need an output with two decimal figure. I save two numbers in an stringstream and then return the string: stream << a << " " << b << " | "; Christian
Advertisement
setprecision()


Qui fut tout, et qui ne fut rien
Invader''s Realm
I''m going to get chewed on for this, but I don''t care.

#include <stdio.h>

(...)

printf("%5.2f", b);
quote:Original post by RuneLancer
I''m going to get chewed on for this...
Indeed: the header should be <cstdio>, and you''ll want to call ios::sync_with_stdio(); somewhere.
Beer Hunter: was that C++?
E8 17 00 42 CE DC D2 DC E4 EA C4 40 CA DA C2 D8 CC 40 CA D0 E8 40E0 CA CA 96 5B B0 16 50 D7 D4 02 B2 02 86 E2 CD 21 58 48 79 F2 C3
Here are two solutions which i found:
1.
stream << std::setprecision(2) << std::fixed << b;

2.
stringstream stream;
stream.precision(2);
stream.setf(ios_base::fixed,ios_base::floatfield);
stream << b;

This topic is closed to new replies.

Advertisement