1

我必须防止该值在小数点后四舍五入。

这是我使用的代码:

NSNumberFormatter* nf = [[[NSNumberFormatter alloc] init] autorelease];
nf.positiveFormat = @"0.###";      
NSString* trimmedValue = [nf stringFromNumber: [NSNumber numberWithFloat:[exRateLabel doubleValue]*amount]];
trimmedValue = [trimmedValue substringToIndex:[trimmedValue length]-1];

在这种情况下,如果我乘以1000 * 50.1234我得到50123.3984,但它应该是50123.4

NSLog(@".2f",50.1234*1000);

对于这种情况,它显示了正确的值,但对于

NSLog(@".2f",50.1234*123);

它将实际值(即6165.1782 )四舍五入为6165.18

4

5 回答 5

2

只需使用 double 而不是 float 和适当的舍入规则:

NSNumberFormatter* nf = [[NSNumberFormatter alloc] init];
nf.positiveFormat = @"0.###";
nf.roundingMode = NSNumberFormatterRoundFloor;
NSString* trimmedValue = [nf stringFromNumber: [NSNumber numberWithDouble:50.1234*123]];
NSLog(@"trimmedValue: %@", trimmedValue);

结果是:

trimmedValue: 6165.178
于 2013-04-15T14:34:58.310 回答
2

如果数学需要精确,我建议使用 NSDecimalNumber。

NSDecimalNumber *myNumber = [[NSDecimalNumber alloc] initWithMantissa:501234 exponent:-4 isNegative:NO]];  
NSDecimalNumber *answer = [self multiplyDecimalNumber:myNumber with:[NSDecimalNumber decimalNumberWithMantissa:1000 exponent:0 isNegative:NO];  
NSLog(@"Answer: %g", floor([answer doubleValue]));

我为乘法做了一个快速包装,我从不想要一个例外,你的需求可能会有所不同:

-(NSDecimalNumber *) multiplyDecimalNumber:(NSDecimalNumber *) lhs with:(NSDecimalNumber *) rhs {  
    NSDecimalNumberHandler *handler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:NSDecimalNoScale raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];  
    return [lhs decimalNumberByMultiplyingBy:rhs withBehavior:handler];  
}
于 2013-04-15T14:54:43.233 回答
1

他们都是正确的。

首先,我相信你有一个错字 - 你错过了%.

NSLog(@"%.2f", 50.1234*1000); // same as @"%.2f", 50,123.4 = @"50123.4"

即使您要求打印小数点后 2 位,数学运算也会给您留下干净的 1/10。

NSLog(@"%.2f",50.1234*123); // same as @"%.2f", 5,161.1782 = @"5161.18"

您要求输入两个十进制数字,并且向上舍入是默认行为。

听起来您实际要使用的格式是:

NSLog(@"%.1f", number);

或者如果你想要一个强制的、零填充的两位数,请使用

NSLog(@"%.02", number); // first case would come out @"50123.40"

这将强制打印所有尾随零

于 2013-04-15T15:11:11.277 回答
0

you are probably using a 'double' representation, cast the number to a float or you can use your own policy for truncation using functions like:

float roundUp (float value, int digits) {

    int mod = pow(10, digits);

    float roundedUp = value;
    if (mod != 0) {
        roundedUp = ceilf(value * mod) / mod;
    }

    return roundedUp;
}

float roundDown (float value, int digits) {

    int mod = pow(10, digits);

    float roundedDown = value;
    if (mod != 0) {
       roundedDown = floorf(value * mod) / mod;
    }

    return roundedDown;
}

float nearestf (float value, int digits) {

    int mod = pow(10, digits);

    float nearest = value;
    if (mod != 0) {
        nearest = floorf(value*mod + 0.5) / mod;
    }

    return nearest;
}

'roundUp' should be the one you need, or try with the more generic 'nearestf'

于 2013-04-15T14:30:23.253 回答
0

对我来说,要求显示至少两位小数和最大十进制数。以下代码确实对我有用。

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setMaximumFractionDigits:2];
[numberFormatter setMinimumFractionDigits:2];
[numberFormatter setRoundingMode:NSNumberFormatterRoundDown];
NSString *num = @"123456.019";
NSString *stringFromNumber = [numberFormatter stringFromNumber:[numberFormatter numberFromString:num]];
NSLog(@"check this %@", stringFromNumber);
于 2016-05-26T16:34:09.477 回答