如何将一种格式转换为另一种格式?

时间:2013-08-09 12:57:23

标签: iphone ios

这是我的Button方法。当我按下按钮时,它的值是每次增加一个&显示到标签。然后它达到6然后转换像1.0,7 = 1.1,8 = 1.2,12像2.0像板球格式。

我该怎么做?

-(void)OneNoBTNPressedMethod
{
    // LBL it's my label & display the text

    NSString * overStorage = LBL.text;

    // perform the addition operation

    CalcOperation operation;

    operation = Plus;

    //add one every time when we press the button

    NSString * overOneBTNStr = [NSString stringWithFormat:@"1"];
    NSString *overVal = overOneBTNStr;

    LBL.text= [NSString stringWithFormat:@"%qi",[overVal longLongValue]+[overStorage longLongValue]];
}

提前完成..

2 个答案:

答案 0 :(得分:0)

试试这个逻辑

Let `int num` is count
Then

numBeforeDecimal = num/6;
numAfterDecimal = num%6;

连接数字

[NSString stringWithFormat: @"%d.%d",numBeforeDecimal,numAfterDecimal];

答案 1 :(得分:0)

它对我有用。希望它也适合你。假设您的LBL包含“1”作为初始文本。希望它有所帮助

- OneNoBTNPressedMethod {
   NSString *str = LBL.text;
   NSArray *arr = [str componentsSeparatedByString:@"."];
   if ([arr count] == 1) {
       if ([LBL.text intValue] >= 5) {
           LBL.text = [NSString stringWithFormat:@"%i.%i",0,0];
       } else {
           LBL.text = [NSString stringWithFormat:@"%i",[LBL.text intValue] + 1];
       }
   } else if ([arr count] == 2) {
       if ([[arr objectAtIndex:1] intValue] >= 5) {
          int left = [[arr objectAtIndex:0] intValue] + 1;
          LBL.text = [NSString stringWithFormat:@"%i.%i",left,0];
       } else {
           LBL.text = [NSString stringWithFormat:@"%i.%i",[[arr objectAtIndex:0] intValue],[[arr objectAtIndex:1] intValue] + 1];
       }
   }
}