C++程式:將數字舍入到n位小數


在任何語言中編寫程式時,在輸出中表示數字是一項有趣且重要的任務。對於整數型別(short、long或medium)資料,很容易將數字表示為輸出。對於浮點數(float或double型別),有時需要將其舍入到一定的位數。例如,如果要將52.24568表示為三位小數,則需要進行一些預處理。在本文中,我們將介紹一些技術,透過舍入將浮點數表示為一定的小數位數。

在不同的方法中,使用類似C語言的格式化字串、使用精度引數以及使用math庫中提供的round()函式非常重要。讓我們逐一看看它們。並附帶正確的語法和程式碼示例。

使用格式化字串

在C語言中,我們使用printf()函式表示格式化列印。要使用printf()函式顯示一些資料,需要預先指定格式化字串。C++中也提供了相同的printf()函式。要將數字表示為一定的小數位數,格式化語法如下所示:

語法

printf語句的語法。

printf ( “%.<number of decimal place>f”, <floating point number> );

例如,如果要將浮點數變數NUM顯示為4位小數,則語句如下:

printf ( “%.4f”, NUM );

示例

#include <iostream> using namespace std; void solve( float number) { printf ( "%.3f", number ); } int main(){ cout << "Number 45.278586 up to 3 decimal places: "; solve( 45.278586 ); }

輸出

Number 45.278586 up to 3 decimal places: 45.279

在這個例子中,我們可以看到給定的數字最多有6位小數。但是我們將其顯示為3位小數。舍入時,它會自動轉換為最接近的值。但是,此過程有一個缺點。我們無法根據需要動態更改小數位數。為了克服這個問題,我們可以使用基於C++的setprecision()方法。

使用setprecision方法

C++有一個名為setprecision()的特殊格式化函式,用於將精度值設定為n位小數。要使用此方法,需要匯入iomanip庫。還需要指定我們使用的是定點小數。語法如下:

語法

setprecision()方法的定義

include <iomanip>
std::cout << std::fixed;
std::cout << std::setprecision( <number of decimal places> );
std::cout << The_floating_point_number;

例如,如果要將浮點數變數NUM顯示為4位小數,則語句如下:

include <iomanip>
std::cout << std::fixed;
std::cout << std::setprecision( 4 );
std::cout << NUM;

示例

#include <iostream> #include <iomanip> using namespace std; void solve( float number, int place) { cout << fixed; cout << setprecision( place ); cout << number << endl; } int main(){ cout << "Number 45.278586 up to 3 decimal places: "; solve( 45.278586, 3); cout << "Number 45.278586 up to 4 decimal places: "; solve( 45.278586, 4); cout << "Number 45.278586 up to 5 decimal places: "; solve( 45.278586, 5); }

輸出

Number 45.278586 up to 3 decimal places: 45.279
Number 45.278586 up to 4 decimal places: 45.2786
Number 45.278586 up to 5 decimal places: 45.27859

這是將數字表示為n位小數的理想方法。有時對於n=0,我們可以使用另一種方法進行舍入。這會將數字轉換為整數。方法如下:

使用round()方法

‘cmath’庫有一個round()方法,用於將數字轉換為最接近的整數。因此,這將浮點數轉換為0位小數。語法如下。

語法

使用round()方法

include <cmath>
float res = round ( <floating point number> );

例如,如果要將數字45.254舍入到最接近的整數,則語句如下。

include <cmath>
float res = round ( 45.254 );
std::cout << res;

示例

#include <iostream> #include <cmath> using namespace std; void solve( float number) { float res; res = round ( number ); cout << res << endl; } int main(){ cout << "Number 45.278586 to its nearest integer: "; solve( 45.278586 ); cout << "Number 89.7854 to its nearest integer: "; solve( 89.7854 ); cout << "Number -45.69 to its nearest integer: "; solve( -45.69 ); }

輸出

Number 45.278586 to its nearest integer: 45
Number 89.7854 to its nearest integer: 90
Number -45.69 to its nearest integer: -46

在這個例子中,很明顯,將浮點數轉換為最接近的整數的合適且簡單的方法是使用round()函式。此函式將數字作為引數,並返回整數等效值。在我們的示例中,有一個負數-45.69,舍入後,它變成了-46,小於此數。因此,round()方法不像floor()或ceil()。

結論

在C++中編寫程式碼時,將浮點數表示為n位小數有幾種方法。最基本的方法是使用帶有格式化字串的printf()方法。但是,對於此方法,格式字串的小數位數無法動態更改。為了解決這個問題,C++的iomanip庫提供了setprecision()方法,該方法需要指定浮點數舍入到的位數。有時需要將浮點數舍入到最接近的整數(0位小數)。在這種情況下,可以使用C++的cmath庫中的round()方法。

更新於:2022年10月17日

21K+ 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告