Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: float precision  (Read 6580 times)

0 Members and 1 Guest are viewing this topic.

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
float precision
« 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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
float precision
« Reply #1 on: September 18, 2008, 08:29:56 pm »
Code: [Select]
out << setprecision(2) << fixed << score;
Laurent Gomila - SFML developer

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
float precision
« Reply #2 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 ===|

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
float precision
« Reply #3 on: September 18, 2008, 09:56:34 pm »
it seems sstreams dont have setprecision

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
float precision
« Reply #4 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;
Laurent Gomila - SFML developer

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
float precision
« Reply #5 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 ?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
float precision
« Reply #6 on: September 18, 2008, 10:22:39 pm »
Code: [Select]
oss.str("");
Laurent Gomila - SFML developer

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
float precision
« Reply #7 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 =)

zarka

  • Jr. Member
  • **
  • Posts: 81
    • View Profile
float precision
« Reply #8 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 :)
//Zzzarka