iPhone:如何截断浮点数而不进行四舍五入?

发布于 2024-12-08 20:34:20 字数 169 浏览 1 评论 0原文

我需要简单地截断浮点数(截断而不是四舍五入)。

float FloatNum = 43.6823921;
NSString *numString = [NSString stringWithFormat:@"%.1f", FloatNum]; // yields 43.7

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

float FloatNum = 43.6823921;
NSString *numString = [NSString stringWithFormat:@"%.1f", FloatNum]; // yields 43.7

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一身骄傲 2024-12-15 20:34:20

您可以采取多种方法,但您的解决方案将取决于许多因素。

这总是会截断到十分位。

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

正如 chown 提到的,您可以将其转换为字符串并获取子字符串。如果您不总是处理两位数数字,您可能需要使用 rangeOfString: os 来查找小数点。

另一种选择是使用 NSDecimalNumber 及其方法 decimalNumberByRoundingAccordingToBehavior: 来显式设置舍入。我曾多次使用此选项来处理货币操纵,其中准确性非常重要。

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;
}

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;
}
月依秋水 2024-12-15 20:34:20

这会将浮点数转换为字符串,获取索引 4 的子字符串,然后转换回浮点数。我认为这是截断浮点而不进行舍入的常用方法:float myTruncatedFloat = [[[[NSNumber numberWithFloat:43.6823921] stringValue] substringToIndex:4] floatValue]; 。这会将 43.6823921 转换为 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);

印刷:

[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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文