2013-01-09 17 views

回答

2

我不熟悉gmplib,但它支持fixed格式化操縱器嗎?

在標準C++:

#include <iostream> 
#include <iomanip> 

using namespace std; 

int main() 
{ 
    double d = 1.12345e6; 

    cout << d << endl; 
    cout << fixed << d << endl; 
    return 0; 
} 

生產:

$ ./test 
1.12345e+06 
1123450.000000 

可以與精度玩setprecision(n)和寬度與setw(n)以進一步調整的結果。

1

實際上,GMPLib提供了很好地打印mpf_t值的方法。見下文。

#include <stdio.h> 
#include <gmp.h> 

int main(int argc, const char * argv[]) { 
    mpf_t f; 
    mpf_init_set_str(f, "314.15926", 10); 
    gmp_printf("%.5Ff\n", f); 
    return 0; 
} 

如果您運行的腳本,控制檯將輸出:

314.15926 

在這裏,最重要的是在格林尼治標準時間浮點格式標誌約5:%.5Ff,這意味着它將輸出5位數的浮點數