Sta*_* As 11 c++ qt localization qt4
如何将数字(双精度)转换为字符串,使用自定义小数点和千位分隔符字符?
我见过QLocale,但我不想选择本地化国家,而是指定我自己的小数点和千位分隔符.
谢谢
Ste*_*Chu 12
Qt不支持自定义区域设置.但是处理只是组和小数点字符是微不足道的:
const QLocale & cLocale = QLocale::c();
QString ss = cLocale.toString(yourDoubleNumber, 'f');
ss.replace(cLocale.groupSeparator(), yourGroupChar);
ss.replace(cLocale.decimalPoint(), yourDecimalPointChar);
顺便说一句,斯波茨的问题并非无关紧要.关于目标的更多细节总是有帮助的,它可能导致可能更好地为您服务的不同方法.
以下是仅使用 std::lib(无 QT)的方法。定义您自己的 numpunct 派生类,它可以指定小数点、分组字符,甚至分组之间的间距。使用包含您的构面的语言环境为 ostringstream 注入。根据需要在该 ostringstream 上设置标志。输出到它并从中获取字符串。
#include <locale>
#include <sstream>
#include <iostream>
class my_punct
    : public std::numpunct<char>
{
protected:
    virtual char do_decimal_point() const {return ',';}
    virtual char do_thousands_sep() const {return '.';}
    virtual std::string do_grouping() const  {return std::string("\2\3");}
};
int main()
{
    std::ostringstream os;
    os.imbue(std::locale(os.getloc(), new my_punct));
    os.precision(2);
    fixed(os);
    double x = 123456789.12;
    os << x;
    std::string s = os.str();
    std::cout << s << '\n';
}
1.234.567.89,12
| 归档时间: | 
 | 
| 查看次数: | 11503 次 | 
| 最近记录: |