简体   繁体   English

在Objective-C(或C)中正确使用和格式化浮点数

[英]Proper Usage and Formatting of a Float in Objective-C (or C)

I have an iPhone app. 我有一个iPhone应用程序。 I am storing a float value, (distance), in my sqlite3 db. 我在sqlite3数据库中存储一个浮点值(距离)。 (The field in the db is formatted to float) I am able to store float values correctly in the db no problem. (db中的字段被格式化为float)我能够在db中正确存储float值没问题。 However, I can't seem to figure out how to pull the value back out of the db the format and present it correctly. 但是,我似乎无法弄清楚如何将值从db格式中拉回并正确显示。 Here is my code for pulling the value out of my db and using it: 这是我的代码,用于从数据库中提取值并使用它:

NSString *itemDistance = [NSString stringWithFormat:@"%f",[item distance]];
float questionDistance = [itemDistance floatValue];

[item distance] is a float value. [项目距离]是一个浮动值。 I can't get this to work. 我无法使它正常工作。 I get a REALLY long value instead. 我得到了一个很长的值。 What am I doing wrong? 我究竟做错了什么? Any help would be greatly appreciated. 任何帮助将不胜感激。

Thank you in advance for your help, 预先感谢您的帮助,

L. L.

Assuming your -distance method is returning the right value, then this sounds like basic misunderstanding of how floats work in C. Common mistake. 假设您的-distance方法返回正确的值,那么这听起来像是对float如何在C中工作的基本误解。常见错误。 Every developer falls into this trap at least once. 每个开发人员至少会陷入此陷阱一次。

Floating point numbers can actually only represent a fairly limited number of values. 浮点数实际上只能表示数量有限的值。 Specifically, unless you happen to choose a value that is exactly representable as a float, you'll get the nearest value, which will often have many decimal places of data. 具体而言,除非您碰巧选择了一个可以精确表示为浮点数的值,否则您将获得最接近的值,该值通常具有许多小数位数据。

The reason for this is because a float is only 32 bits; 原因是浮点数只有32位; 4 bytes. 4字节。 Now, how many numbers with decimal points are there between 0..1000000 or 0..1000 or, even, 0..1. 现在,在0..1000000或0..1000或什至0..1之间有多少个带小数点的数字。 Infinite. 无穷。 Floats implement a very finite subset of possible numeric values and do so in a way where the resulting possible values may have many decimal places. 浮点数实现了可能的数值的非常有限的子集,并且以可能的结果值可能具有许多小数位的方式实现。

Consider: 考虑:

printf("%5.18f\n", (float) 2.05);
printf("%5.18f\n", (float) 2.45);
printf("%5.18f\n", (float) 4200.75);
printf("%5.18f\n", (float) 37.89);
printf("%5.18f\n", (float) 1.2);
printf("%5.18f\n", (float) -1.2);

This prints: 打印:

2.049999952316284180
2.450000047683715820
4200.750000000000000000
37.889999389648437500
1.200000047683715820
-1.200000047683715820

Those values are as close to the values in the bit of code that a float could represent. 这些值与浮点数可以表示的代码位中的值非常接近。

If you need more precision, use a double. 如果需要更高的精度,请使用双精度。 More precision than that? 比这更精确? Use NSDecimalNumber. 使用NSDecimalNumber。

And, if you can, use CoreData. 并且,如果可以,请使用CoreData。 It makes managing this kind of thing a bit more straightforward. 它使管理这类事情变得更加简单。

why go round in circles? 为什么绕圈转?

NSString *itemDistance = [NSString stringWithFormat:@"%.2f",[item distance]];
float questionDistance = [itemDistance floatValue];

Where the .2 is saying I want 2 decimal places. .2表示我想要2个小数位。

Whats the type of of distance? 什么是距离类型? If it is an instance of NSNumber then I dont think the code you wrote will work, try 如果它是NSNumber的实例,那么我认为您编写的代码无效,请尝试

NSString *itemDistance = [[item distance] stringValue];
float questionDistance = [itemDistance floatValue];

or even 甚至

float q=[[item distance] floatValue];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM