如何在Objective-C中执行字符串转换? [英] How to do string conversions in Objective-C?

查看:82
本文介绍了如何在Objective-C中执行字符串转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个字符串转换为双精度,并在做一些数学后,将其转换回一个字符串。



如何在Objective-

解决方案

/ div>

可以使用

将NSString转换为双精度。

  double myDouble = [myString doubleValue]; 

舍入到最接近的整数可以做为

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

我真的不知道是否有更精简的方式转换回

字符串

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


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

How do I do this in Objective-C?

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

解决方案

You can convert an NSString into a double with

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];

这篇关于如何在Objective-C中执行字符串转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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