[英]How to compare NSNumber with a decimal value?
I recently upgraded from iOS 5 to iOS 6 and found this unit test was failing 我最近从iOS 5升级到iOS 6,发现此单元测试失败
- (void)testCalculatesDistanceBetweenTwoPoints
{
self.sut = [[DistanceCalculator alloc] init];
CLLocationCoordinate2D start = {.latitude = 34.32, .longitude = 99.13};
CLLocationCoordinate2D finish = {.latitude = 105.94, .longitude = 27.73};
NSNumber *distance = [self.sut kilometresBetweenPlace1:start andPlace2:finish];
NSNumber *expected = [NSNumber numberWithDouble:3822.23073702318];
STAssertEqualObjects(distance, expected, @"");
}
The failing asserting 失败的断言
'3822.23073702318' should be equal to '3822.23073702318'
'3822.23073702318'应该等于'3822.23073702318'
When I print the raw value of each number I get what looks like the same value 当我打印每个数字的原始值时,我得到的看起来像是相同的值
2012-10-23 20:01:42.970 HelloWorld[1573:c07] 1 3822.23073702318
2012-10-23 20:01:42.970 HelloWorld[1573:c07] 2 3822.23073702318
When I print the type of each number I get what looks like the same (could I be doing this wrong?) 当我打印每个数字的类型时,我得到的看起来像是一样的(我做错了吗?)
2012-10-23 20:06:37.309 HelloWorld[1611:c07] 1 __NSCFNumber
2012-10-23 20:06:37.309 HelloWorld[1611:c07] 2 __NSCFNumber
Here is the full blown implementation if that helps 如果有帮助,这里是完整的实现
- (NSNumber *)kilometresBetweenPlace1:(CLLocationCoordinate2D)place1 andPlace2:(CLLocationCoordinate2D)place2
{
MKMapPoint start = MKMapPointForCoordinate(place1);
MKMapPoint finish = MKMapPointForCoordinate(place2);
double distance = MKMetersBetweenMapPoints(start, finish) / 1600;
return [NSNumber numberWithDouble:distance];
}
For anyone who might follow this discussion -here is what I ended up with 对于任何可能关注此讨论的人-这就是我最终得到的结果
- (void)testCalculatesDistanceBetweenTwoPoints
{
double accuracy = 0.00000000001;
self.sut = [[DistanceCalculator alloc] init];
CLLocationCoordinate2D start = {.latitude = 34.32, .longitude = 99.13};
CLLocationCoordinate2D finish = {.latitude = 105.94, .longitude = 27.73};
NSNumber *distance = [self.sut kilometresBetweenPlace1:start andPlace2:finish];
NSNumber *expected = [NSNumber numberWithDouble:3822.23073702318];
STAssertEqualsWithAccuracy([distance doubleValue], [expected doubleValue], accuracy, @"");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.