IOS 關於四舍五入的神器--NSDecimalNumber


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

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

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

 

-(NSString *)notRounding:(float)price afterPoint:(int)position{

    NSDecimalNumberHandler* roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:position raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];

    NSDecimalNumber *ouncesDecimal;

    NSDecimalNumber *roundedOunces;

    

    ouncesDecimal = [[NSDecimalNumber alloc] initWithFloat:price];

    roundedOunces = [ouncesDecimal decimalNumberByRoundingAccordingToBehavior:roundingBehavior];

    [ouncesDecimal release];

    return [NSString stringWithFormat:@"%@",roundedOunces];

}

介紹一下參數:

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該怎么做?,在開發文檔上有這樣一個表,是按照保留小數點后一位處理的。相信大家一看就明白了:

 

方法二:

1round(12345.6789) 結果為:12346

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

第二個是我要的結果,但是我不明白這么個簡單的四舍五入要搞的這么復雜,應該有更好的吧,我記得在其他語言里用:round(12345.67892) 就可以實現四舍五入到兩位小數。

 

 

方法三:

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  1
1 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;    //3.5
    cout << "7.0 / 2   = " << 7.0/2 << endl;    //3.5
    cout << "7.0 / 2.0 = " << 7.0/2.0 << endl; //3.5
    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)
{
    double dIn = dInput;
    if (dInput >= 0.0)    //???
    {
        return int(dIn + 0.5);
    } 
    else
    {
        return int(dIn - 0.5);
    }
}

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;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM