0

I want to know the proper way to convert from "Float" to "std::wstring".

Xaml file

<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 File

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);

}

That's my piece of code throwing me that error:

E0415   no suitable constructor exists to convert from "float" to "std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t>>"

1 Answer 1

1
#include <sstream>
std::wstringstream wss;
wss << 1.4f;
auto result = wss.str(); // what you want
Sign up to request clarification or add additional context in comments.

1 Comment

That's how we did things more than a decade ago. C++11 introduced std::to_wstring, in other words: auto const result{ std::to_wstring(1.4f) };.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.