Objective-C将Long和Float转换为String [英] Objective-c convert Long and float to String

查看:92
本文介绍了Objective-C将Long和Float转换为String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Objective-C中将两个数字转换为字符串.

I need to convert two numbers to string in Objective-C.

一个是长号,另一个是浮点数.

One is a long number and the other is a float.

我在互联网上搜索了一个解决方案,每个人都使用stringWithFormat:,但我无法使其正常工作.

I searched on the internet for a solution and everyone uses stringWithFormat: but I can't make it work.

我尝试

NSString *myString = [NSString stringWithFormat: @"%f", floatValue]

12345678.1234 ,并获得"12345678.00000" 作为输出

NSString *myString = [NSString stringWithFormat: @"%d", longValue]

有人可以告诉我如何正确使用stringWithFormat:吗?

Can somebody show me how to use stringWithFormat: correctly?

推荐答案

本文讨论如何使用各种格式的字符串将数字/对象转换为NSString实例:

This article discusses how to use various formatting strings to convert numbers/objects into NSString instances:

字符串编程指南:格式化字符串对象

使用此处指定的格式:

字符串编程指南:字符串格式说明符

想要浮动,

[NSString stringWithFormat:@"%1.6f", floatValue]

并持续很长时间:

[NSString stringWithFormat:@"%ld", longValue] // Use %lu for unsigned longs

但是说实话,有时使用NSNumber类会更容易:

But honestly, it's sometimes easier to just use the NSNumber class:

[[NSNumber numberWithFloat:floatValue] stringValue];
[[NSNumber numberWithLong:longValue] stringValue];

这篇关于Objective-C将Long和Float转换为String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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