SFML community forums

Help => Graphics => Topic started by: Bogdan on February 12, 2015, 07:07:48 pm

Title: Thousand separator in sfml
Post by: Bogdan on February 12, 2015, 07:07:48 pm
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;
}
Title: Re: Thousand separator in sfml
Post by: Jesper Juhl on February 12, 2015, 07:12:55 pm
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/
Title: Re: Thousand separator in sfml
Post by: Laurent on February 12, 2015, 08:12:13 pm
std::ostringstream oss;
oss.imbue(loc);
oss << std::fixed << var << std::endl;
text.setString(oss.str());
Title: Re: Thousand separator in sfml
Post by: Bogdan on February 17, 2015, 06:27:32 pm
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?

Title: Re: Thousand separator in sfml
Post by: Laurent on February 17, 2015, 06:40:16 pm
http://www.cplusplus.com/iomanip
Title: Re: Thousand separator in sfml
Post by: Jesper Juhl on February 17, 2015, 07:43:49 pm
http://en.cppreference.com/w/cpp/io/manip
http://en.cppreference.com/w/cpp/header/iomanip
Title: Re: Thousand separator in sfml
Post by: Bogdan on February 18, 2015, 10:32:02 am
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;
}
Title: Re: Thousand separator in sfml
Post by: Laurent on February 19, 2015, 03:34:13 pm
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.
Title: Re: Thousand separator in sfml
Post by: Bogdan on February 19, 2015, 06:03:36 pm
Ok, it worked, thanks