c++ - 在 C++ 中将 wchar_t 转换为 float

标签 c++ typeconverter wchar-t

是否有更好的方法将 wchar_t 转换为 float ?我只找到了一种实际有效的解决方案。

wchar_t buf = GetText(); // GetText() returns a wchar_t
float fNumber1 = _wtof(&buf);

我也尝试过以下失败:

float fNumber1 = (float)GetText(); // returns the ascii code

float fNumber1 = _wtof(&GetText();

float fNumber1 = _wtof(&(GetText())); 

最佳答案

#include <wchar.h>

int main ()
{
  wchar_t szOrbits[] = L"90613.305 365.24";
  wchar_t * pEnd;
  long double f1, f2;
  f1 = wcstold (szOrbits,&pEnd);
  f2 = wcstold (pEnd,NULL);
  wprintf (L"Pluto takes %.2Lf years to complete an orbit.\n", f1/f2);
  return 0;
}

关于c++ - 在 C++ 中将 wchar_t 转换为 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21948429/

相关文章:

c++ - 如何将 std::istream 转换为 std::wistream

c++ - 使用 unix 终端运行 c++ 程序

winapi - Win32 : Determine whether stdout handle is char or wchar stream

html - 如何在 Golang 中将 HTML 表单值转换为 int

c# - bool 转换问题的枚举

c - asprintf 有 wchar_t 版本吗?

.net - 我如何将 const wchar_t* 转换为 System::String?

c++ - 类模板特化,C++,模板参数列表中的参数 2 不匹配

c++ - 允许编译器优化堆内存分配吗?

flutter - 将列表转换为 Json 字符串,然后将此字符串转换回 Dart 中的列表