如何将字符串转换为双精度,反之亦然? [英] How to convert a string into double and vice versa?

查看:76
本文介绍了如何将字符串转换为双精度,反之亦然?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将字符串转换为双精度值,然后对其进行一些数学运算,然后将其转换回字符串.

I want to convert a string into a double and after doing some math on it, convert it back to a string.

我如何在 Objective-C 中做到这一点?

How do I do this in Objective-C?

有没有办法将双精度四舍五入到最接近的整数?

Is there a way to round a double to the nearest integer too?

推荐答案

您可以使用

double myDouble = [myString doubleValue];

四舍五入到最近的整数可以这样完成

Rounding to the nearest int can then be done as

int myInt = (int)(myDouble + (myDouble>0 ? 0.5 : -0.5))

老实说,我不确定是否有比

I'm honestly not sure if there's a more streamlined way to convert back into a string than

NSString* myNewString = [NSString stringWithFormat:@"%d", myInt];

这篇关于如何将字符串转换为双精度,反之亦然?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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