首页 > 解决方案 > C++ 小数点

问题描述

我有这个功能:

#include <cstdlib>
#include <clocale>
double SimplifiedExample () {
  setlocale (LC_ALL, "");
  char txt [] = "3.14";
  for (int x=0; txt [x]; x++) 
    if (txt [x] == '.')
      txt [x] = localeconv ()->decimal_point [0];

  char *endptr;
  return strtod (txt, &endptr);
}

这适用于我尝试过的每个 Windows 10 和 Linux 系统。它在至少一台 Windows 7 机器上失败(它为 localeconv ()->decimal_point [0] 报告“”,但在 sprintf 和 strtod 中都使用“.”)。使用 localeconv 为 strtod 提供系统小数点是否正确?

请注意,我无法删除 setlocale() 调用。应用程序必须尊重区域设置。

标签: c++

解决方案


你试过“man strtod”吗?我有 Slackware linux,strtod 手册页有 strtod,它提到了小数点的语言环境,并说它位于 stdlib.h 中。


推荐阅读