c ++ float to const wchar_t * conversion function

时间:2016-11-20 12:12:33

标签: c++

我正在将一个浮点数转换为一个const wchar_t *

DisplayText(ConversionUtils::FloatToWstring(fps).c_str()); // Prints garbage

DisplayText(std::to_wstring(fps).c_str()); // Doesn't print anything to the device.

使用此功能:

std::wstring ConversionUtils::FloatToWstring(float value) {

    return std::to_wstring(value);
}

我希望得到类似的东西:

DisplayText(ConversionUtils::FloatToConstWcharPtr(fps));

1 个答案:

答案 0 :(得分:4)

只需按价值返回:

std::wstring ConversionUtils::FloatToWchar(float value) {
    std::string str = std::to_string(value);
    return std::wstring(str.begin(), str.end());
}

或者更好,请改用std::to_wstring()