Skip to main content
gamedev.net
🔒 Locked

double format

Started by Austrian Coder 4 replies 300+ views
Original Post
Austrian Coder
Austrian Coder
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
RuneLancer
RuneLancer
I''m going to get chewed on for this, but I don''t care.

#include <stdio.h>

(...)

printf("%5.2f", b);
Beer Hunter
Beer Hunter
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.
Jan Wassenberg
Jan Wassenberg
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
Austrian Coder
Austrian Coder
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;

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.