c++ - 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() 调用。应用程序必须尊重区域设置。
解决方案
你试过“man strtod”吗?我有 Slackware linux,strtod 手册页有 strtod,它提到了小数点的语言环境,并说它位于 stdlib.h 中。
推荐阅读
- android - 无故自动返回上一个活动画面
- python - 新版 fasttext 中的 predict_proba
- angular - 在 Cordova/Ionic 4 移动应用程序中提供原生广告?
- git - 如何在没有私钥的情况下添加具有 SSH URL 的子模块
- context-free-grammar - 模糊的 CFG 是否有可能转换为 CNF 并变得明确?
- java - API针对不同场景返回不同的对象类型
- nodes - 节点的 Networkx 和 matplotlib 颜色图;不同网络的相同地图
- html - 是否可以将多个视频加载到 HTML 5 视频播放器中并让它们一个接一个地按顺序播放?
- javascript - 如何停止跟踪 AdRoll Pixel?
- python - 在python中完成系列(数据框)