我有一个我设置为4.5的浮动,但是当我将它渲染到屏幕时它显示为4.500000。我已经尝试使用谷歌搜索和搜索stackoverflow,但所有的答案都使用printf()或std :: cout我无法使用,因为我不想将它输出到控制台。对于nooby问题感到抱歉,这是代码:
TextRenderer::render(*window, std::to_string(player->health) + " HP", sf::Vector2f(3, 0), 16, sf::Color(155, 0, 0));
编辑:TextRenderer.cpp
#include "TextRenderer.h"
#include <SFML/Graphics.hpp>
#include <string>
#include "Logger.h"
sf::Font TextRenderer::kavoon = sf::Font();
TextRenderer::TextRenderer()
{
}
TextRenderer::~TextRenderer()
{
}
bool TextRenderer::load() {
if (!TextRenderer::kavoon.loadFromFile("data/fonts/kavoon.ttf"))
{
Logger::error("Couldn't load kavoon font.");
}
return true;
}
void TextRenderer::render(sf::RenderWindow& window, std::string string, sf::Vector2f position, int size, sf::Color color) {
sf::Text text;
text.setFont(TextRenderer::kavoon);
text.setString(string);
text.setPosition(position);
text.setCharacterSize(size);
text.setColor(color);
window.draw(text);
}
答案 0 :(得分:3)
您可以使用ostringstream执行此操作。
ostringstream ss;
ss << player->health;
string health = ss.str();
然后
TextRenderer::render(*window, health + " HP", sf::Vector2f(3, 0), 16, sf::Color(155, 0, 0));