SFML community forums

Help => General => Topic started by: ravenheart on September 18, 2008, 06:28:22 pm

Title: float precision
Post by: ravenheart on September 18, 2008, 06:28:22 pm
hello , i got a float and want to display it in the form  numbers. XX
 i did it this way:
float  score = Spieluhr.GetElapsedTime();
        out2.setf(ios::precision(2)) << Spieluhr.GetElapsedTime() ;
        str2 = out2.str();
        Text.SetText(str2);
        out2.seekp(0) ;

out is a sstream, out << setprecision(2) doesnt work, too
Text is a String
Title: float precision
Post by: Laurent on September 18, 2008, 08:29:56 pm
Code: [Select]
out << setprecision(2) << fixed << score;
Title: float precision
Post by: ravenheart on September 18, 2008, 09:54:45 pm
well now same again it seems:

C:\Users\Isis\Documents\C++\MEINS\Pong_new\main.cpp||In function `int main()':|
C:\Users\Isis\Documents\C++\MEINS\Pong_new\main.cpp|180|error: `[color=darkred]setprecision[/color]' was not declared in this scope|
C:\Users\Isis\Documents\C++\MEINS\Pong_new\main.cpp|180|warning: unused variable 'setprecision'|
||=== Build finished: 1 errors, 1 warnings ===|
Title: float precision
Post by: ravenheart on September 18, 2008, 09:56:34 pm
it seems sstreams dont have setprecision
Title: float precision
Post by: Laurent on September 18, 2008, 10:02:02 pm
It's in header <iomanip> and namespace std.

Code: [Select]
#include <iomanip>
#include <sstream>

std::ostringstream out;
float score = 0.1234567f;
out << std::setprecision(2) << std::fixed << score;
Title: float precision
Post by: ravenheart on September 18, 2008, 10:18:47 pm
well now that works, but now i have another prblem:

in out there is the current score, when a new game is started

the timer is at 0.00, when the game befor had lets say 111.11 s, then
in the new game 0.0011 is shown because i only use out.seekp(0) to get new data in it how can i clear a ostringstream ?
Title: float precision
Post by: Laurent on September 18, 2008, 10:22:39 pm
Code: [Select]
oss.str("");
Title: float precision
Post by: ravenheart on September 18, 2008, 10:26:57 pm
so i can modify the content of the stream and force it to be anything i want?

i thought its only to get what is already in it ^^

but now it works fine , thank you

=============================
as u see there are many beginners questions
not really related to sfml thats because i learn
c++ by myself and only java at university
thanks again =)
Title: float precision
Post by: zarka on September 19, 2008, 04:22:48 pm
Quote from: "ravenheart"
well now that works, but now i have another prblem:

in out there is the current score, when a new game is started

the timer is at 0.00, when the game befor had lets say 111.11 s, then
in the new game 0.0011 is shown because i only use out.seekp(0) to get new data in it how can i clear a ostringstream ?


you could also create new stringstreams every time :) ..
Code: [Select]

int main()
{
  float myval = 0.5f;
  std::string myval_str;
  {
    std::stringstream ss;
    ss << myval;
    myval_str = ss.str();
  }
}


scopes can have their uses :)