Objective-C将Long和Float转换为String [英] Objective-c convert Long and float to 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屋!