2013-03-01 119 views
2

在我的项目中,我有一个函数可以在我的界面上运行一个5位数的数字并将其输出到标签中。的数量可以是一个双键或一个浮子,但我想它,以显示像### + ##。##自定义格式化文本标签的字符串值

实例号12345.67

所示像123 + 45.67

的输出将总是是一个带小数的5位数字。我研究了格式化数据格式的具体数字格式,但没有遇到任何具体的这种情况。这是我将数字更改为字符串并将其分配给标签的地方。请提前帮助并感谢您的时间。

NSString *outputNumber = [NSString stringWithFormat:@"%f",numberHere]; 

_stationing.text = outputNumber; 

回答

1

有几种方法可以做到这一点。

NSMutableString *outputNumber = [[NSString stringWithFormat:@"%.2f", numberHere] mutableCopy]; 
[outputNumber insertString:@"+" atIndex:outputNumber.length - 5]; 

_stationing.text = outputNumber; 

另:

int high = numberHere/100; 
float low = numberHere - (high * 100); 
NSString *outputNumber = [NSString stringWithFormat:@"%d+%.2f", high, low]; 

如果数字的格式是这些方法不会,如果数量少于100

+0

好答案,谢谢。我选择了后一个选项bc,这对我来说更有意义。我会将它标记为已回答,但我的代表还不够高。 – JBeesky 2013-03-01 00:54:54

0

希望这有助于

NSStirng *numberString = [NSString stringWithFormat:@"%.2f",numberHere]; 

NSString *firstPart = [numberString substringWithRange:NSMakeRange(0, 3)]; 
NSString *secondPart [numberString substringWithRange:NSMakeRange(3, 5)]; 

NSString *outputString = [NSString stringWithFormat:@"%@+%@", firstPart, secondPart]; 

正常工作不是永久的,你应该先检查长度。

+0

您需要使用'%.2f'作为格式来获得所需的两位小数。 – rmaddy 2013-03-01 00:47:38

+0

感谢您的回复。我和Maddy的回答一起,BC对我来说是一个简单易行的代码。 – JBeesky 2013-03-01 00:56:17