double variable

Hello, how I can do that double variable shows only 2 numbers after dot let say:
20.00
not:
20.00000
by the way im using sfml engine :D
code:
1
2
3
4
5
6
7
8
9
10
  double money = 20;
....
         sf::Text walletsk;
         walletsk.setFont(calibri);
         walletsk.setCharacterSize(18);
         walletsk.setFillColor(sf::Color::White);
         walletsk.setPosition(sf::Vector2f(660, 25));
         walletsk.setString(to_string(money));
...
window.draw(walletsk);

and i get result:
https://i.imgur.com/aQzy0g7.png
Last edited on
There is no way to change the format that std::to_lower use. You might want to use std::ostringstream instead.

ostringstream is a class that allows you output things just like cout but instead of outputting to the screen you get it as a string.

1
2
3
std::ostringstream oss;
oss << std::fixed << std::setprecision(2) << money;
walletsk.setString(oss.str());
side note but money is often an integer and often 1 is the smallest currency type, for example in $USA that is 1 cent = 1. That way you can avoid floating point problems with money which can be very, very bad when dealing with real money.

you can display the current value in floating point as above, using a precision of 2 and dividing the integer by the conversion (100.0, in this case).

Topic archived. No new replies allowed.