如何防止从字符串中获取doubleValue时舍入? [英] How to prevent the rounding off when getting doubleValue from string?

查看:103
本文介绍了如何防止从字符串中获取doubleValue时舍入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从服务器获取经纬度是字符串格式的JSON,然后通过使用默认的doubleValue函数将经纬度的值转换为double值来使用此经度和纬度,但是通过使用此值进行四舍五入。那么,如何防止从字符串获取doubleValue时四舍五入?
这里是我的代码

  CLLocationCoordinate2D defaultLoc; 

defaultLoc.latitude = [[self.obj valueForKey:@latitude] doubleValue];
defaultLoc.longitude = [[self.obj valueForKey:@longitude] doubleValue];

NSLog(@latitude:%f longitude:%f,defaultLoc.latitude,defaultLoc.longitude);


解决方案

格式说明符%f 默认情况下将double值舍入为小数点后六位。尝试指定格式为%。10f 或任何您需要的小数位数。

I am getting latitude and longitude from server in JSON which is in string format then i am using this latitude and longitude by converting the values of latitude and longitude in double value by using default doubleValue function but by using this value is rounded off. So, How can i prevent the rounding off when getting doubleValue from string? Here is my code

  CLLocationCoordinate2D defaultLoc;

            defaultLoc.latitude = [[self.obj valueForKey:@"latitude"] doubleValue];
            defaultLoc.longitude = [[self.obj valueForKey:@"longitude"] doubleValue]; 

            NSLog(@"latitude : %f longitude : %f",defaultLoc.latitude,defaultLoc.longitude);

解决方案

The format specifier %f rounds the double values to six decimals by default. Try specifing the format as %.10for whatever number of decimals you need.

这篇关于如何防止从字符串中获取doubleValue时舍入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆