English 中文(简体)
iPhone - 将双倍值推入一个只有7个精度的NSDictionary
原标题:iPhone - Storing a double value into a NSDictionary with only 7 decimals as a Number type

I have a PList that contains a dictionary with some decimal values like 10.1234567, stored as Number type (tag ).
In my app, I read that plist and manipulate those values as NSNumber (double type values) with a custom class and its accessors :

@interface
...
@property(nonatomic, assign) NSNumber* someValue;
...
@end

@implementation
...
- (NSNumber*) someValue { return [self.dict objectForKey:@"val"]; }
- (void) setSomeValue:(NSNumber*)val { [self.dict setValue:val forKey:@"val"]; }
...
@end

在某些情况下,我重新在磁盘上写了该名单。

几个小时前,我注意到,这些价值已经转变,现为10.12345669999999999。 ......

Woaw,我没有预料这一变化。

Well, I ve tried to contain the problem changing the accessors with many thing, like for example this one :

- (NSNumber*) someValue { return [self.dict objectForKey:@"val"]; }
- (void) setSomeValue:(NSNumber*)val { [self.dict setValue:
                                              [NSNumber numberWithDouble:[[NSString stringWithFormat:@"%.7lf", [val doubleValue]] doubleValue]] 
                                                          forKey:@"val"]; }

但是,我可以成功地把这些价值留在原来的7分位。

我怎么可能只写7个小小数,而不必将数字类别以外的东西用于字典(我指的是,用“强硬”作为好的解决办法)?

这份清单文件是用手制作的,有几小时的打字,打上了每个领域的7个伊斯兰价值观的打字,当时它们是“变相”的。 他们储存着7名mal。 因此,我猜测,在用方案写回这7名神职人员时,可以找到解决办法。

问题回答

2. 欢迎进入一个可贵的浮动世界。 如果你想要使用浮动值,那么你就必须做好准备,因为浮动点值很少准确反映你们想要的价值观,但更经常是一发。

您可使用体格(视需要按比例计算),或改写为品。 有一个准确的NSDecimalNumber类别,但不适合NSNumber计划,而且如果有必要,我将不与JSON等人合作。 不过,我认为,如果把工作作为关键词。

• 在你从名单上抽取NSNumber时使用NSNumberFormatter,应当:

NSNumberFormatter *nf = [[[NSNumberFormatter alloc] init] autorelease];
[nf setMaximumFractionDigits:7];
NSNumber *yourNumber = [nf numberFromString:[[self someValue] stringValue]];




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签