【问题标题】:Universal App VS2015 C++ How to convert String^ to float?Universal App VS2015 C++ 如何将String^转换为float?
【发布时间】:2015-10-29 09:34:30
【问题描述】:

我正在使用Visual studio 2015 通过C++ 构建一个通用应用程序; 当我想将Textbox->Text 转换为float 类型时。我搜索谷歌,但没有解决。我不能使用TryParse or Convert:: or float.parse。 这是我的Mainpage.cpp的用法

using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;

我不能使用System::。当我写它时显示syntax error

【问题讨论】:

  • 您正在编写纯本机代码,不能使用 .NET Framework 命名空间或方法。如果您更喜欢转换为 wstring,请使用普通的 C++ 转换函数,例如 _wtof(someString->Data) 或 stod()。
  • 感谢您的回复。我这样写: float a = _wtof(textBox->Text->Data);并得到一个错误 C3867: 'Platform::String::Data': non-standard syntax;使用 App1(我的项目名称)'&' 创建指向成员的指针。我该如何解决?
  • Data是一个方法,所以使用Data()
  • 哦,我的错!谢谢@PeterTorr-MSFT

标签: win-universal-app c++-cx


【解决方案1】:

试试下面的代码。

String^ text = "3.14";

std::wstring textToConvert = std::wstring(text->Data());

float value = std::stof(textToConvert);

【讨论】:

    猜你喜欢
    • 2011-12-22
    • 2019-12-11
    • 2011-11-02
    • 2015-12-17
    • 1970-01-01
    • 1970-01-01
    • 2014-06-28
    • 2012-11-11
    • 1970-01-01
    相关资源
    最近更新 更多