I have a
我有一个
float a = 1412.244019;
and I need a to be rounded to the nearest second decimal like 1412.24000000. I understand that if i want to present a with only two decimals i use %.2f, but that is NOT the case. I need the new a for mathematical reasons as a float.
我需要把a四舍五入到最近的第二个小数,比如1412。24000000。我知道,如果我想用两个小数表示a,我用%。2f,但事实并非如此。我需要新的a作为浮点数。
I have tried a couple of methods found on * without luck. The more obvious one i used, i mention below, but still had no luck using it. Can YOU see why the magic is not happening?
我已经尝试了一些在*上没有运气的方法。下面是我使用过的最明显的一个,但我还是没能使用它。你能明白为什么魔术没有发生吗?
PS: It does do the desired effect sometimes, NOT always, which gets me thinking... hmmm...
PS:它有时确实能达到预期的效果,但并不总是如此,这让我思考……嗯…
Thanks in advance.
提前谢谢。
Code:
代码:
float a = 1412.244019;
NSLog(@"a is %f", a); //output a is 1412.244019
a = [[NSString stringWithFormat:@"%.2f", a] floatValue];
NSLog(@"a is %f", a); //output a is 1412.239990
EDIT:
编辑:
SEEMS like when i am using the float a after the above surgery, it is considering it to be 1412.240000 eventhough the NSLog says differently... strange. But i am getting what I want, so Kudos for wasted time chasing nothing :)
似乎我在做了上述手术后使用float a时,它被认为是1412.24万,尽管NSLog的说法不同……奇怪。但是我得到了我想要的,所以浪费时间去追求什么都没有是值得的。
EDIT I would love to choose you all as correct answers, but since i can only choose one, i chose the first good answer with extra explanation (of the two last).
编辑我很乐意选择你们所有人作为正确的答案,但是因为我只能选择一个,所以我选择了第一个好的答案,并给出了额外的解释(最后两个)。
6 个解决方案
#1
50
Have you tried this?
你有试过吗?
CGFloat val = 37.777779;
CGFloat rounded_down = floorf(val * 100) / 100; /* Result: 37.77 */
CGFloat nearest = floorf(val * 100 + 0.5) / 100; /* Result: 37.78 */
CGFloat rounded_up = ceilf(val * 100) / 100; /* Result: 37.78 */
source : Rounding Number to 2 Decimal Places in C
来源:四舍五入数字到小数点后两位
Complementing: You just don't have control about how the computer will store the float value.
补充:你无法控制计算机如何存储浮动值。
So these rounded values may not be EXACTLY the "obvious" decimal values, but they will be very very close, and that's the max guarantee you will have.
所以这些四舍五入的值可能不完全是“明显”的十进制值,但它们会非常接近,这是最大保证。
#2
10
You do that in Objective-C exactly the same as you would in C:
你在Objective-C中做的和在C中做的完全一样
double notRounded = ...;
double rounded = round (notRounded * 100.0) / 100.0;
Don't use float - unless you can explain to someone exactly what your specific reasons are to use float over double. float has very limited precision. And using ceil or floor is daft, since there is a perfectly fine standard function named "round".
不要使用浮点数——除非你能向某人解释你使用浮点数的具体原因。浮动精度非常有限。使用ceil或floor是愚蠢的,因为有一个完美的标准函数叫做“round”。
Neither float nor double can exactly represent most decimal values, like 12.24. Because it has much higher precision, double will let you get much closer to 12.24. Therefore, there is a huge chance that NSLog will display some weird number for (float) 12.24, like 12.23999997394 or whatever, while it may display 12.24 for double.
浮点数和双精度数都不能精确地表示大多数十进制数,比如12.24。因为它有更高的精度,双精度会让你更接近12。24。因此,NSLog将显示一些奇怪的数字(浮点数)12.24,比如12.23999997394或其他,而它可能显示12.24表示双精度。
#3
5
The closest value to 1412.24 that a float
can have is 1412.239990234375. There is no operation that can produce a float
any closer than that, because the format of float
objects does not represent any closer values. Asking to represent 1412.24 in float
is like asking to represent 2.5 in int
. It simply cannot be done.
浮动最接近1412.24的值是1412.239990234375。没有任何操作可以产生浮点数,因为浮点对象的格式不代表任何更接近的值。请求在浮点数中表示1412.24就像请求在int中表示2.5一样,这是不可能的。
Options for dealing with this include:
处理这一问题的备选办法包括:
- You can use use formatting that displays “1412.24”, without changing the underlying
float
object. - 您可以使用显示“1412.24”的使用格式,而不需要更改底层的浮动对象。
- You can use
double
to get closer (much closer), but it will still not be exactly 1412.24. - 你可以使用double来接近(更接近),但它仍然不是1412.24。
- You can scale
float
values to representable values (e.g., measure in other units so that the values you want are all exactly representable infloat
). - 您可以将浮点值缩放到可表示的值(例如,以其他单位度量,以便您想要的值在float中都是完全可表示的)。
- You can use a different data type, such as
NSDecimal
. - 可以使用不同的数据类型,如NSDecimal。
#4
3
Error in this Line
这条线误差
a = [[NSString stringWithFormat:@"%.2f", a] floatValue];
correct it to
纠正它
a = [NSString stringWithFormat:@"%.02f", a];
#5
2
You could multiply a by 100.0 and perhaps add 0.5 for rounding and then take the int value of the result. You can then use / 100 to get the value before the decimal point and % 100 to get the two decimal places.
你可以把a乘以100。0,也许加上0。5来表示四舍五入,然后取结果的int值。然后可以使用/ 100在小数点前获取值,使用% 100获取两个小数点后的值。
#6
2
You can't force a float value. Floats are designed to be somewhat approximate, as their magnitude can be great, and since you've only got so many bits to use, it can get close to, but not exactly express a decimal number.
不能强制一个浮点数。浮动被设计成某种近似,因为它们的大小可以是很大的,而且由于您只有那么多的位可以使用,它可以接近,但不能准确地表示一个十进制数。
One suggestion is to do all your arithmetic in integer, multiplied by 100, then dividing when you want to display your final result. You may have numbers too large which would prohibit this.
一个建议是把所有的算术都写成整数,乘以100,然后在显示最终结果时再进行除法。你的数字可能太大了,这是禁止的。
#1
50
Have you tried this?
你有试过吗?
CGFloat val = 37.777779;
CGFloat rounded_down = floorf(val * 100) / 100; /* Result: 37.77 */
CGFloat nearest = floorf(val * 100 + 0.5) / 100; /* Result: 37.78 */
CGFloat rounded_up = ceilf(val * 100) / 100; /* Result: 37.78 */
source : Rounding Number to 2 Decimal Places in C
来源:四舍五入数字到小数点后两位
Complementing: You just don't have control about how the computer will store the float value.
补充:你无法控制计算机如何存储浮动值。
So these rounded values may not be EXACTLY the "obvious" decimal values, but they will be very very close, and that's the max guarantee you will have.
所以这些四舍五入的值可能不完全是“明显”的十进制值,但它们会非常接近,这是最大保证。
#2
10
You do that in Objective-C exactly the same as you would in C:
你在Objective-C中做的和在C中做的完全一样
double notRounded = ...;
double rounded = round (notRounded * 100.0) / 100.0;
Don't use float - unless you can explain to someone exactly what your specific reasons are to use float over double. float has very limited precision. And using ceil or floor is daft, since there is a perfectly fine standard function named "round".
不要使用浮点数——除非你能向某人解释你使用浮点数的具体原因。浮动精度非常有限。使用ceil或floor是愚蠢的,因为有一个完美的标准函数叫做“round”。
Neither float nor double can exactly represent most decimal values, like 12.24. Because it has much higher precision, double will let you get much closer to 12.24. Therefore, there is a huge chance that NSLog will display some weird number for (float) 12.24, like 12.23999997394 or whatever, while it may display 12.24 for double.
浮点数和双精度数都不能精确地表示大多数十进制数,比如12.24。因为它有更高的精度,双精度会让你更接近12。24。因此,NSLog将显示一些奇怪的数字(浮点数)12.24,比如12.23999997394或其他,而它可能显示12.24表示双精度。
#3
5
The closest value to 1412.24 that a float
can have is 1412.239990234375. There is no operation that can produce a float
any closer than that, because the format of float
objects does not represent any closer values. Asking to represent 1412.24 in float
is like asking to represent 2.5 in int
. It simply cannot be done.
浮动最接近1412.24的值是1412.239990234375。没有任何操作可以产生浮点数,因为浮点对象的格式不代表任何更接近的值。请求在浮点数中表示1412.24就像请求在int中表示2.5一样,这是不可能的。
Options for dealing with this include:
处理这一问题的备选办法包括:
- You can use use formatting that displays “1412.24”, without changing the underlying
float
object. - 您可以使用显示“1412.24”的使用格式,而不需要更改底层的浮动对象。
- You can use
double
to get closer (much closer), but it will still not be exactly 1412.24. - 你可以使用double来接近(更接近),但它仍然不是1412.24。
- You can scale
float
values to representable values (e.g., measure in other units so that the values you want are all exactly representable infloat
). - 您可以将浮点值缩放到可表示的值(例如,以其他单位度量,以便您想要的值在float中都是完全可表示的)。
- You can use a different data type, such as
NSDecimal
. - 可以使用不同的数据类型,如NSDecimal。
#4
3
Error in this Line
这条线误差
a = [[NSString stringWithFormat:@"%.2f", a] floatValue];
correct it to
纠正它
a = [NSString stringWithFormat:@"%.02f", a];
#5
2
You could multiply a by 100.0 and perhaps add 0.5 for rounding and then take the int value of the result. You can then use / 100 to get the value before the decimal point and % 100 to get the two decimal places.
你可以把a乘以100。0,也许加上0。5来表示四舍五入,然后取结果的int值。然后可以使用/ 100在小数点前获取值,使用% 100获取两个小数点后的值。
#6
2
You can't force a float value. Floats are designed to be somewhat approximate, as their magnitude can be great, and since you've only got so many bits to use, it can get close to, but not exactly express a decimal number.
不能强制一个浮点数。浮动被设计成某种近似,因为它们的大小可以是很大的,而且由于您只有那么多的位可以使用,它可以接近,但不能准确地表示一个十进制数。
One suggestion is to do all your arithmetic in integer, multiplied by 100, then dividing when you want to display your final result. You may have numbers too large which would prohibit this.
一个建议是把所有的算术都写成整数,乘以100,然后在显示最终结果时再进行除法。你的数字可能太大了,这是禁止的。