Objective-C 中的四舍五入数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/752817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:24:14  来源:igfitidea点击:

Rounding numbers in Objective-C

objective-ccocoamacosformattingrounding

提问by Ash

I'm trying to do some number rounding and conversion to a string to enhance the output in an Objective-C program.

我正在尝试进行一些数字舍入并转换为字符串以增强 Objective-C 程序中的输出。

I have a float value that I'd like to round to the nearest .5 and then use it to set the text on a label.

我有一个浮点值,我想四舍五入到最接近的 0.5,然后用它来设置标签上的文本。

For example:

例如:

1.4 would be a string of: 1.5

1.4 将是一串:1.5

1.2 would be a string of: 1

1.2 将是一串:1

0.2 would be a string of: 0

0.2 将是一个字符串:0

I've spent a while looking on Google for an answer but, being a noob with Objective-C, I'm not sure what to search for! So, I'd really appreciate a pointer in the right direction!

我花了一段时间在 Google 上寻找答案,但是作为 Objective-C 的菜鸟,我不确定要搜索什么!所以,我真的很感激一个指向正确方向的指针!

Thanks, Ash

谢谢,阿什

回答by Ash

Thanks for the pointers everyone, I've managed to come up with a solution:

感谢大家的指点,我已经设法想出了一个解决方案:

float roundedValue = round(2.0f * number) / 2.0f;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:1];
[formatter setRoundingMode: NSNumberFormatterRoundDown];

NSString *numberString = [formatter stringFromNumber:[NSNumber numberWithFloat:roundedValue]];
[formatter release];

The above works for the test cases I threw at it, but if anyone knows a better way to do this I'd be interested to hear it!

以上适用于我投入的测试用例,但如果有人知道更好的方法来做到这一点,我很想听听!

回答by Durai Amuthan.H

float floatVal = 1.23456;

Rounding

四舍五入

int roundedVal = lroundf(floatVal); 

NSLog(@"%d",roundedVal);

Rounding Up

围捕

int roundedUpVal = ceil(floatVal); 

NSLog(@"%d",roundedUpVal);

Rounding Down

四舍五入

int roundedDownVal = floor(floatVal);

NSLog(@"%d",roundedDownVal);

回答by keremk

NSString *numberString = [NSString stringWithFormat:@"%f", round(2.0f * number) / 2.0f];

回答by keremk

Use lroundf() to round a float to integer and then convert the integer to a string.

使用 lroundf() 将浮点数舍入为整数,然后将整数转换为字符串。

回答by Simpu

NSString *numberString = [NSString stringWithFormat:@"%d",lroundf(number)];

回答by hbw

I'd recommend looking into using NSNumberFormatter.

我建议考虑使用NSNumberFormatter

回答by Lal Krishna

a simple way:

一个简单的方法:

float theFloat = 1.23456;
int rounded = roundf(theFloat); NSLog(@"%d",rounded);
int roundedUp = ceil(theFloat); NSLog(@"%d",roundedUp);
int roundedDown = floor(theFloat); NSLog(@"%d",roundedDown);
// Note: int can be replaced by float

回答by Vladimirs Matusevics

NSString *intNumberString = [NSString stringWithFormat:@"%d", (int)floatNumber];

回答by ProgrammingNinja

Following Technique worked for me in a financial application.

以下技术在金融应​​用程序中对我来说有效。

 NSString *dd=[NSString stringWithFormat:@"%0.2f",monthlyPaymentCalculated];

        monthlyPaymentCalculated=[dd doubleValue];

self.monthlyPaymentCritical=monthlyPaymentCalculated;

what is did is first is rounded it with %0.2f and stored it in NSString then i simply converted it again into double and result was good for my calculation.

所做的是首先用 %0.2f 将其四舍五入并将其存储在 NSString 中,然后我简单地将其再次转换为 double 并且结果对我的计算有好处。

回答by Stunner

I needed to be able to round to a specific digit (not necessarily to whole integers). I made a NSNumbercategory (based off Ash's answer) and added the following method to it:

我需要能够四舍五入到一个特定的数字(不一定是整数)。我创建了一个NSNumber类别(基于Ash 的回答)并添加了以下方法:

- (NSString *)stringByRounding:(NSNumberFormatterRoundingMode)roundingMode
      toPositionRightOfDecimal:(NSUInteger)position
{
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setMaximumFractionDigits:position];
    [formatter setRoundingMode:roundingMode];
    NSString *numberString = [formatter stringFromNumber:self];
    return numberString;
}

Which allows me to use it like so:

这允许我像这样使用它:

[aNumber stringByRounding:NSNumberFormatterRoundUp toPositionRightOfDecimal:2];

I can use it to round to integers by passing in 0as the second parameter:

我可以使用它通过0作为第二个参数传入来四舍五入为整数:

[aNumber stringByRounding:NSNumberFormatterRoundPlain toPositionRightOfDecimal:0];