-
Hi,
is there any way to format numbers in sfml, for example with the thousand separator?
In the following example the number is formatted correctly in the console but how to implement it into sfml? Could not find any solution for it so far.
I guess this line is wrong/incomplete?: numbertext.setString(toString<unsigned long long>(var));
Thx in advance for any help.
#include <SFML/Graphics.hpp>
#include <string>
#include <sstream>
#include <locale>
#include <iostream>
template <typename T>
std::string toString(T arg)
{
std::stringstream ss;
ss << arg;
return ss.str();
}
int main()
{
sf::RenderWindow mMainWindow(sf::VideoMode(500, 500), "Window");
mMainWindow.setFramerateLimit(40);
#ifdef _WIN32
std::locale loc("English_America");
#else
std::locale loc("en_US.UTF8");
#endif
unsigned long long var = 100000000;
sf::Font myFont;
myFont.loadFromFile("Arial.ttf");
sf::Text numbertext(toString<unsigned long long>(var), myFont);
while (mMainWindow.isOpen())
{
sf::Event event;
bool click = false;
sf::Vector2i mousePos;
while (mMainWindow.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
mMainWindow.close();
break;
case sf::Event::MouseButtonPressed:
if (event.mouseButton.button == sf::Mouse::Left)
{
click = true;
mousePos = sf::Vector2i (event.mouseButton.x, event.mouseButton.y);
break;
}
}
}
if(click)
{
var = var *1.5;
std::cout.imbue(loc);
std::cout << std::fixed << var << '\n';
numbertext.setString(toString<unsigned long long>(var));
}
mMainWindow.clear();
mMainWindow.draw(numbertext);
mMainWindow.display();
}
return 0;
}
-
On my phone, so I can't test at the moment. But wouldn't this do?
numbertext.setString(std::to_string(var));
Or use the .imbue() function on the stringstream to set the proper locale for the string, I guess..
This may also help: http://www.cplusplus.com/articles/D9j2Nwbp/
-
std::ostringstream oss;
oss.imbue(loc);
oss << std::fixed << var << std::endl;
text.setString(oss.str());
-
Is it also possible to "right-align" the number for the rightmost digit to stay at a fixed position, when it grows by a digit, instead of expanding to the right?
-
http://www.cplusplus.com/iomanip
-
http://en.cppreference.com/w/cpp/io/manip
http://en.cppreference.com/w/cpp/header/iomanip
-
In the console it works perfectly and in sfml it's better now (center-aligned?), but still not right-aligned:
What's wrong with the code?
#include <SFML/Graphics.hpp>
#include <string>
#include <sstream>
#include <locale>
#include <iostream>
#include <iomanip>
template <typename T>
std::string toString(T arg)
{
std::stringstream ss;
ss << arg;
return ss.str();
}
int main()
{
sf::RenderWindow mMainWindow(sf::VideoMode(500, 500), "Window");
mMainWindow.setFramerateLimit(40);
#ifdef _WIN32
std::locale loc("English_America");
#else
std::locale loc("en_US.UTF8");
#endif
unsigned long long var = 100000000;
sf::Font myFont;
myFont.loadFromFile("Arial.ttf");
sf::Text numbertext(toString<unsigned long long>(var), myFont);
numbertext.setPosition(100,0);
std::ostringstream oss;
oss.imbue(loc);
oss << std::fixed << std::setw(24) << std::right << var << std::endl;
numbertext.setString(oss.str());
while (mMainWindow.isOpen())
{
sf::Event event;
bool click = false;
sf::Vector2i mousePos;
while (mMainWindow.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
mMainWindow.close();
break;
case sf::Event::MouseButtonPressed:
if (event.mouseButton.button == sf::Mouse::Left)
{
click = true;
mousePos = sf::Vector2i (event.mouseButton.x, event.mouseButton.y);
break;
}
}
}
if(click)
{
var = var *1.5;
std::ostringstream oss;
oss.imbue(loc);
oss << std::fixed << std::setw(24) << std::right << var << std::endl;
numbertext.setString(oss.str());
std::cout << std::fixed << std::setw(24) << std::right << var << std::endl;
}
mMainWindow.clear();
mMainWindow.draw(numbertext);
mMainWindow.display();
}
return 0;
}
-
Not sure if that's your problem, but you should use a monospace font if you want the alignment to be accurate like in the console.
-
Ok, it worked, thanks