首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> C++ >> 關於C++

C++ 浮點數格式化輸出:精確控制小數位數的最佳實踐

     編輯:關於C++

問題場景

日常開發中(對話框、數據庫、金融、短信等)頻繁需要將浮點數按指定小數位格式化為字符串,如 123.45678 → "123.45"。標準庫無直接支持,需自行實現。

兩大「真言」

  1. 需格式化數值時,先轉為字符串,保證每位數佔一字符
  2. 轉字符串請用 <sstream> 庫(ostringstream)

接口設計

string do_fraction(long double value, int decplaces = 3);
// decplaces 包含小數點,故保留 2 位小數傳 3

關鍵技術點

1. 精度陷阱與解決

stringstream 默認精度 6 位(總位數,非小數位),大數會科學計數法輸出。須設為最大精度:

int prec = numeric_limits<long double>::digits10; // 18
ostringstream out;
out.precision(prec);
out << value;
string str = out.str();

2. 小數點定位與截斷

const char DECIMAL_POINT = '.'; // 歐洲用 ','
size_t n = str.find(DECIMAL_POINT);
if (n != string::npos && str.size() > n + decplaces) {
    str[n + decplaces] = '\0'; // 覆蓋多餘位
}

3. 移除尾部垃圾字符

利用 c_str() 遇 \0 截斷特性,配合 swap 縮減字符串:

str.swap(string(str.c_str())); // 老編譯器可用 str = string(str.c_str());

完整實現

string do_fraction(long double value, int decplaces = 3) {
    ostringstream out;
    out.precision(numeric_limits<long double>::digits10);
    out << value;
    string str = out.str();

    size_t n = str.find('.');
    if (n != string::npos && str.size() > n + decplaces) {
        str[n + decplaces] = '\0';
    }
    str.swap(string(str.c_str()));
    return str;
}

進階用法

若不想拷貝返回,可改引用傳遞:void do_fraction(long double, string&, int)。但返回值允許鏈式調用:cout << do_fraction(123456789.69999) << '\t' << do_fraction(12.011); 輸出 123456789.69 12.01。

總結

通過 <sstream> 高精度轉字符串 + find 定位 + \0 截斷 + swap 清理,實現高效、彈性、無科學計數法干擾的浮點數格式化。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved