IOS 關於四捨五入的

2021-07-01 22:29:18 字數 4160 閱讀 7090

如何只舍不入。比如 float price = 0.126,怎麼樣才能得到0.12?

當然,通過字串擷取的辦法肯定也能達到相同的效果。但是就是這麼乙個簡單的問題要通過一些判斷和擷取才能獲得結果,總感覺有點笨拙。

下面先給出該問題的解決辦法:

-(nsstring *)notrounding:(float)price afterpoint:(int)position

介紹一下引數:

price:需要處理的數字,

position:保留小數點第幾位,

然後呼叫

float s =0.126;

nsstring *sv = [self notrounding:s afterpoint:2];

nslog(@"sv = %@",sv);

輸出結果為:sv = 0.12

接下來介紹nsdecimalnumberhandler初始化時的關鍵引數:decimalnumberhandlerwithroundingmode:nsrounddown,

nsrounddown代表的就是 只舍不入。

scale的引數position代表保留小數點後幾位。

如果只入不捨怎麼辦,比如,float 0.162 想要得到0.17該怎麼做?,在開發文件上有這樣乙個表,是按照保留小數點後一位處理的。相信大家一看就明白了:

方法二: 1

、round(12345.6789) 

結果為:

12346

2、round(12345.6789*100)/100 結果為:12345.68

第二個是我要的結果,但是我不明白這麼個簡單的四捨五入要搞的這麼複雜,應該有更好的吧,我記得在其他語言裡用:round(

12345.6789,2

) 就可以實現四捨五入到兩位小數。

方法三:

nstimeinterval interval = 

305.721125;

nsinteger timeint = [[nsstring

stringwithformat:@"%.0f", interval] integervalue];

interval:305.721125

timeint:306

1

nslog(@"%@", [nsstring stringwithformat:@"%.0f", 1.0003]);

2

nslog(@"%@", [nsstring stringwithformat:@"%.0f", 1.9003]);

3

nslog(@"%@", [nsstring stringwithformat:@"%.0f", 1.5003]);

4

nslog(@"%@", [nsstring stringwithformat:@"%.0f", 1.4003]);

1

11

1

1

2

1

2

1

1

方法四:

1. /

//test "/"

cout << "test \"/\"!" << endl;

cout << "7   / 2   = " << 7/2 << endl;      //3

cout << "7   / 2.0 = " << 7/2.0 << endl;   

cout << "7.0 / 2   = " << 7.0/2 << endl;   

cout << "7.0 / 2.0 = " << 7.0/2.0 << endl;

cout << "7   / 3   = " << 7/3 << endl;      //2

cout << endl;

2. %

//test "%"

cout << "test \"%\"!" << endl;

cout << "9   % 3   = " << 9%3 << endl;      //0

cout << "9   % 4   = " << 9%4 << endl;      //1

//cout << "9.0 % 3   = " << 9.0%3 << endl;

//cout << "9   % 3.0 = " << 9%3.0 << endl;

cout << endl;

3. 四捨五入

//test round()

cout << "test \"四捨五入\"!" << endl;

double drounda = 1.4;

double droundb = 1.6;

double droundlowa = -1.4;

double droundlowb = -1.6;

double droundlowc = 0.0;

cout << drounda << " = " << roundex(drounda) << endl;         //1

cout << droundb << " = " << roundex(droundb) << endl;         //2

cout << droundlowa << " = " << roundex(droundlowa) << endl;   //-1

cout << droundlowb << " = " << roundex(droundlowb) << endl;   //-2

cout << droundlowc << " = " << roundex(droundlowc) << endl;   //0

cout << endl;

double roundex(const double& dinput)

else

}4. ceil() 向上取整

//test ceil() 向上取整

cout << "test ceil() 向上取整!" << endl; 

cout << "ceil 1.2 = " << ceil(1.2) << endl;      //2

cout << "ceil 1.8 = " << ceil(1.8) << endl;      //2

cout << "ceil -1.2 = " << ceil(-1.2) << endl;    //-1

cout << "ceil -1.8 = " << ceil(-1.8) << endl;    //-1

cout << "ceil 0.0 = " << ceil(0.0) << endl;      //0

cout << endl;

5. floor() 向下取整

//test floor() 向下取整

cout << "test floor() 向下取整!" << endl;

cout << "floor 1.2 = " << floor(1.2) << endl;    //1

cout << "floor 1.8 = " << floor(1.8) << endl;    //1

cout << "floor -1.2 = " << floor(-1.2) << endl; //-2

cout << "floor -1.8 = " << floor(-1.8) << endl; //-2

cout << "floor 0.0 = " << floor(0.0) << endl;    //0

cout << endl;

關於四捨五入

四捨五入 轉http nssoft.showdoc.asp?did 267 delphi的四捨五入函式round,對於 5的情況,整數部分是奇數,那麼會round up,偶數會round down,例如 x round 17.5 x 18 x round 12.5 x 12 請使用下面的函式代替ro...

qt的四捨五入 Qt(C )四捨五入

qt現在是四捨六入五成雙,要想四捨五入得自己想辦法,實現如下 include include double c1 3.435 double c2 3.445 double c3 3.4351 double c4 3.4451 double c5 3.445 qdebug qdebug qdebug ...

mysql 金額 四捨五入 mysql四捨五入

雲資料庫 mysql 雲資料庫 rds for mysql 是穩定可靠 可彈性伸縮的雲資料庫服務。通過雲資料庫能夠讓您幾分鐘內完成資料庫部署。雲端完全託管,讓您專注於應用程式開發,無需為資料庫運維煩惱 低至 0.24 小時起,買1年83折 雲資料庫mysql 雲資料庫 rds for mysql 是...