开发者

iPhone: How do you truncate a float without rounding?

I need to simply truncate a floating point number (truncate and not round it).

float FloatNum = 43.682开发者_运维知识库3921;
NSString *numString = [NSString stringWithFormat:@"%.1f", FloatNum]; // yields 43.7


There are many ways you can do, but your solution is going to depends on a number of factors.

This will always truncate to the tenths place.

float floatNum = 43.6823921;
float truncatedFloat = truncf(floatNum * 10) / 10;

As chown mentioned, you can convert it to a string and take the substring. You might want to use rangeOfString: os something to find the decimal if you don't always deal with double digit numbers.

Another option would be to use NSDecimalNumber with it's method decimalNumberByRoundingAccordingToBehavior: to set rounding explicitly. I've used this option a few times to handle currency manipulations where accuracy is very important.

float num = 43.6894589;
NSDecimalNumber *decimalNumber = [NSDecimalNumber decimalNumberWithDecimal:[[NSNumber numberWithFloat:num] decimalValue]];
float truncatedFloat = [[decimalNumber decimalNumberByRoundingAccordingToBehavior:self] floatValue];

// NSDecimalNumberBehaviors
- (NSDecimalNumber *)exceptionDuringOperation:(SEL)method error:(NSCalculationError)error leftOperand:(NSDecimalNumber *)leftOperand rightOperand:(NSDecimalNumber *)rightOperand {
    return [NSDecimalNumber notANumber];
}

- (short)scale {
    return 1;
}

- (NSRoundingMode)roundingMode {
    return NSRoundDown;
}


This will convert a float to string, get the substring to index 4, the convert back to a float. I think this is the usual way of truncating a float without rounding at all: float myTruncatedFloat = [[[[NSNumber numberWithFloat:43.6823921] stringValue] substringToIndex:4] floatValue];. This will convert 43.6823921 into 43.68.

float myfl = 43.6823921;
NSNumber *num = [NSNumber numberWithFloat:myfl];
NSString *numStr = [num stringValue];
NSLog(@"%@", numStr);
NSLog(@"%@", [numStr substringToIndex:2]);
NSLog(@"%@", [numStr substringToIndex:3]);
NSLog(@"%@", [numStr substringToIndex:4]);
NSLog(@"%@", [numStr substringToIndex:5]);    
NSLog(@"%@", [numStr substringToIndex:6]);
NSLog(@"%@", [numStr substringToIndex:7]);
NSLog(@"%@", [numStr substringToIndex:8]);
float newMyFloat = [[numStr substringToIndex:4] floatValue];
NSLog(@"%.1f", newMyFloat);
newMyFloat = [[numStr substringToIndex:5] floatValue];
NSLog(@"%.2f", newMyFloat);

Prints:

[AppDelegate application:didFinishLaunchingWithOptions:]: 43.68239
[AppDelegate application:didFinishLaunchingWithOptions:]: 43
[AppDelegate application:didFinishLaunchingWithOptions:]: 43.
[AppDelegate application:didFinishLaunchingWithOptions:]: 43.6
[AppDelegate application:didFinishLaunchingWithOptions:]: 43.68
[AppDelegate application:didFinishLaunchingWithOptions:]: 43.682
[AppDelegate application:didFinishLaunchingWithOptions:]: 43.6823
[AppDelegate application:didFinishLaunchingWithOptions:]: 43.68239
[AppDelegate application:didFinishLaunchingWithOptions:]: 43.6
[AppDelegate application:didFinishLaunchingWithOptions:]: 43.68
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜