xcode 取整值 iOS 中的浮点值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8017270/
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-14 22:20:15  来源:igfitidea点击:

Round value the float value in iOS

iphoneobjective-ciosxcodensnumber

提问by iDev

I am developing an app and want to round off values i.e if the output is 4.8 I want to display 4.8 while if the output is 4.0 , I want to display 4

我正在开发一个应用程序并想要四舍五入值,即如果输出是 4.8 我想显示 4.8 而如果输出是 4.0 我想显示 4

Also, it would be great if I could precisely round values : as in if value is 4.34 then round to 4.3 while if its 4.37 then round it to 4.4

此外,如果我能精确地舍入值,那就太好了:如果值是 4.34,则舍入为 4.3,而如果值为 4.37,则舍入为 4.4

回答by Nerdtron

One way to round floating point values is to just add 0.5 and then truncate the value.

舍入浮点值的一种方法是添加 0.5,然后截断该值。

double valueToRound = GetTheValueFromSomewhere();
double roundedValue = (double)((int)(valueToRound + 0.5));

This will round 1.4 down to 1.0 and 1.5 up to 2.0 for example. To round to other decimal places as you mentioned, simply multiply the initial value by 10, or 100, etc. use the same sort of code, and then divide the result by the same number and you'll get the same result at whatever decimal place you want.

例如,这会将 1.4 向下舍入为 1.0,将 1.5 向上舍入为 2.0。要四舍五入到您提到的其他小数位,只需将初始值乘以 10 或 100 等。使用相同类型的代码,然后将结果除以相同的数字,无论小数点如何,您都会得到相同的结果你想要的地方。

Here's an example for rounding at an arbitrary precision.

这是以任意精度舍入的示例。

double valueToRound = GetTheValueFromSomewhere();
int decimalPrecisionAtWhichToRound = 0;
double scale = 10^decimalPrecisionAtWhichToRound;
double tmp = valueToRound * scale;
tmp = (double)((int)(tmp + 0.5));
double roundedValue = tmp / scale;

So, if decimalPrecisionAtWhichToRound is set to 0 as in the above it'll round to the nearest whole integer. 1.4 will round to 1.0. 1.5 will round to 2.0.

因此,如果如上所示将 decimalPrecisionAtWhichToRound 设置为 0,它将四舍五入到最接近的整数。1.4 将舍入为 1.0。1.5 将舍入为 2.0。

If you set decimalPrecisionAtWhichToRound to 1, it would round to the nearest tenth. 1.45 would round to 1.5 and 1.43 would round to 1.4.

如果您将 decimalPrecisionAtWhichToRound 设置为 1,它将四舍五入到最接近的十分之一。1.45 将四舍五入为 1.5,1.43 将四舍五入为 1.4。

回答by Hot Licks

You need to first understand how to do rounding on paper, without someone showing you the code to do it. Write down some numbers and figure out how to round them.

您需要首先了解如何在纸上进行四舍五入,而不需要有人向您展示代码。写下一些数字并弄清楚如何将它们四舍五入。

To round to a specific decimal position you add half the value of that position and then truncate. Ie, 1.67 + 0.05 = 1.72 then truncate to 1.7.

要四舍五入到特定的小数位,您添加该位置值的一半,然后截断。即,1.67 + 0.05 = 1.72 然后截断为 1.7。

But there are two tricky things in programming that aren't there when you do it on paper:

但是,当您在纸上进行编程时,编程中有两件棘手的事情是不存在的:

  1. Knowing how to truncate -- There are several ways to do it while programming, but they're non-trivial.
  2. Dealing with the fact that floating-point numbers are imprecise. Ie, there is no exact representation of, say, 1.7, but rather the two closest numbers are apt to be something like 1.69998 and 1.700001
  1. 知道如何截断——在编程时有几种方法可以做到这一点,但它们并不重要。
  2. 处理浮点数不精确的事实。即,没有精确的表示,比如 1.7,但两个最接近的数字很可能是 1.69998 和 1.700001 之类的东西

For truncating the trick of multiplying the number by the appropriate power of 10 to produce an integer works pretty well. Eg, (1.67 + 0.05) * 10 = 17.2, then convert to int to get 17, then convert back to float and divide by 10 to get 1.7 (more or less). Or (if you're printing or displaying the value) just format the integer number with the decimal point inserted. (By formatting the integer value you don't have to deal with the problem of imprecise floating point representations.)

对于截断将数字乘以适当的 10 次幂以生成整数的技巧非常有效。例如,(1.67 + 0.05) * 10 = 17.2,然后转换为 int 得到 17,然后转换回 float 并除以 10 得到 1.7(或多或少)。或者(如果您正在打印或显示值)只需使用插入的小数点格式化整数。(通过格式化整数值,您不必处理浮点表示不精确的问题。)

If you want to suppress trailing zeros it gets a bit trickier and you probably have to actually write some code -- format the number, then scan backwards and take off any trailing zeros up to the decimal point. (And take the decimal point too, if you wish.)

如果你想抑制尾随零,它会变得有点棘手,你可能必须实际编写一些代码——格式化数字,然后向后扫描并去除任何尾随零直到小数点。(如果你愿意,也可以取小数点。)

回答by Waqas Haider Sheikh

    float number=17.125;
NSNumberFormatter *format = [[NSNumberFormatter alloc]init];
[format setNumberStyle:NSNumberFormatterDecimalStyle];
[format setRoundingMode:NSNumberFormatterRoundHalfUp];
[format setMaximumFractionDigits:2];
NSString *temp = [format stringFromNumber:[NSNumber numberWithFloat:number]];
NSLog(@"%@",temp);

回答by rob mayoff

double myNumber = 7.99;
NSString *formattedNumber = [NSString stringWithFormat:@"%.*f",
    fmod(round(myNumber * 10), 10) ? 1 : 0, myNumber];