ios - 为什么我的函数输出一个看起来像内存地址的随机值?

标签 ios sqlite double nsnumber

我正在尝试输出我从 sql 表中查询的纬度和经度的值。我的问题是,当值为 0.000000 时,它会输出类似于 1.04480899798659e-276 的内容。 我检查了这两行代码上面的输出值:

latitudeOutput.text = [[NSNumber numberWithDouble:[theGPS latitude]] stringValue];
longitudeOutput.text = [[NSNumber numberWithDouble:[theGPS longitude]] stringValue];

它绝对应该输出 0.000000 。有谁知道为什么会这样,无论如何我可以解决它?

----添加的部分----

为什么这个 if else 语句直接进入 else 部分,尽管纬度和经度值等于 0.000000?

if(latitude == @"0.000000" && longitude == @"0.000000"){
    latitudeOutput.textColor = [UIColor grayColor];
    longitudeOutput.textColor = [UIColor grayColor];
    latitudeOutput.text = @"Latitude";
    longitudeOutput.text = @"Longitude";
}
else {
    latitudeOutput.text = latitude;
    longitudeOutput.text = longitude;
}

最佳答案

内存地址的格式为0xdeadbeef

您的值是用 scientfic notation. 的“变体”写的 double 值它非常接近 0.0 (wolfram alpha visualization)

发生这种情况是因为计算机存储浮点值的方式。您可以阅读更多here.

要解决您的问题,我建议将输出格式化为只包含您需要的小数点后的数字。

NSString* formattedNumber = [NSString stringWithFormat:@"%.02f", [theGPS latitude]];

这将为您提供小数点后 2 位。

关于ios - 为什么我的函数输出一个看起来像内存地址的随机值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32070016/

相关文章:

c# - 自定义控件绑定(bind)不起作用

ios - SQL WHERE IN 子句不起作用 - FMDB - 传递逗号分隔的字符串

java - 使用 sqlite 将字符串读入数据库

c++ - 如何将 double 转换为字符串?

C++ 程序自动舍入数组中除第一个值以外的 double 值

java - 关于大数

iOS 将数据库更新移出主线程 - 插入启动 View

ios - UITableView:未收到单元格的第一次点击

ios - flutter 运行-> 错误 : No named parameter with the name 'keyboardDismissBehavior'

sqlite - SQLite 中的 sqlite3.connect 和 close 有多贵?