大佬教程收集整理的这篇文章主要介绍了c – 浮动到std :: string转换和本地化,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想知道上面的代码是否可以在Germal语言环境下以“1234,5678”而不是“1234.5678”的形式产生输出,例如:
std::string MyClass::doubleToString(double value) const { char fmtbuf[256],buf[256]; snprintf(fmtbuf,sizeof(fmtbuf)-1,"%s",getDoubleFormat().c_str()); fmtbuf[sizeof(fmtbuf)-1] = 0; snprintf(buf,sizeof(buf)-1,fmtbuf,value); buf[sizeof(buf)-1] = 0; return std::string(buf); } static std::string const& getDoubleFormat() { return "%f"; }
如果是,如何防止这种情况?如何在表单中输出:“1234.5678”用点来分隔小数?
<locale>
本地化会影响
// On program startup,the locale SELEcted is the "C" standard locale,(equivalent to english). printf("Locale is: %s\n",setlocale(LC_ALL,NULL)); cout << doubleToString(3.14)<<endl; // Switch to system specific locale setlocale(LC_ALL,""); // depends on your environment setTings. printf("Locale is: %s\n",NULL)); cout << doubleToString(3.14) << endl; printf("Locale is: %c\n",localeconv()->thousands_sep); printf("decimal separator is: %s\n",localeconv()->decimal_point); // get the point
结果是:
Locale is: C 3.140000 Locale is: French_France.1252 3,140000 decimal separator is:,
如果您选择C格式化功能,那么C++ <locale>
功能更强大,更灵活.但请注意,C语言环境的设置不会影响C语言环境:
cout << 3.14<<" "<<endl; // it's still the "C" local cout.imbue(locale("")); // you can set the locale only for one stream if Desired cout << 3.14 << " "<<1000000<< endl; // localized output
备注:
以下是一个问题:
static std::string const& getDoubleFormat() { return "%f"; }
该函数应返回对字符串的引用.不幸的是,你返回一个字符串文字“%f”,其类型为const char [3].这意味着存在一个隐式转换,它将从const char *构造一个临时字符串并返回其引用.但临时对象在表达式结束时被销毁,因此返回的引用不再有效!
为了测试,我按价值返回.
以上是大佬教程为你收集整理的c – 浮动到std :: string转换和本地化全部内容,希望文章能够帮你解决c – 浮动到std :: string转换和本地化所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。