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

Author Topic: Thousand separator in sfml  (Read 2529 times)

0 Members and 1 Guest are viewing this topic.

Bogdan

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Thousand separator in sfml
« 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;
}

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Thousand separator in sfml
« Reply #1 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/
« Last Edit: February 12, 2015, 07:18:50 pm by Jesper Juhl »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Thousand separator in sfml
« Reply #2 on: February 12, 2015, 08:12:13 pm »
std::ostringstream oss;
oss.imbue(loc);
oss << std::fixed << var << std::endl;
text.setString(oss.str());
Laurent Gomila - SFML developer

Bogdan

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Thousand separator in sfml
« Reply #3 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?


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Thousand separator in sfml
« Reply #4 on: February 17, 2015, 06:40:16 pm »
Laurent Gomila - SFML developer

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
« Last Edit: February 17, 2015, 07:47:02 pm by Jesper Juhl »

Bogdan

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Thousand separator in sfml
« Reply #6 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;
}
« Last Edit: February 19, 2015, 03:07:40 pm by Bogdan »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Thousand separator in sfml
« Reply #7 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.
Laurent Gomila - SFML developer

Bogdan

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Thousand separator in sfml
« Reply #8 on: February 19, 2015, 06:03:36 pm »
Ok, it worked, thanks

 

anything