如何从 float 转换为 std::wstring

How to convert from float to std::wstring

我想知道从“Float”转换为“std::wstring”的正确方法。

Xaml 文件

<Page
    x:Class="TD2_WinUI3.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TD2_WinUI3"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
        
        <Slider AutomationProperties.Name="Slider with ticks" Header="headerString" Width="300" TickFrequency="10" TickPlacement="Outside" ValueChanged="slider_ValueChanged" />
        <TextBlock x:Name="textBlock1" Margin="0,0,0,10" Text="Current value: 0" />
        
    </StackPanel>
    
    
</Page>

Cpp 文件

void MainPage::slider_ValueChanged(Windows::Foundation::IInspectable const&, Microsoft::UI::Xaml::Controls::Primitives::RangeBaseValueChangedEventArgs const& args)
{
    std::wstring msg = static_cast<float>(L"Current value: {0:s}", args.NewValue());
    textBlock1().Text(msg);

}

那是我的代码抛出错误:

E0415   no suitable constructor exists to convert from "float" to "std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t>>"
#include <sstream>
std::wstringstream wss;
wss << 1.4f;
auto result = wss.str(); // what you want