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;

 

posted @ 2012-12-18 12:51  yingkong1987  阅读(8372)  评论(8编辑  收藏  举报