如何将浮点值转换为UIAlertView

时间:2013-01-13 11:20:23

标签: ios objective-c cocoa-touch uialertview

我试图找到将浮点值转换为UIAlertView的正确方法。我的float值仅用于检查另一个值,而不是传递给某处的字符串。

我想我可以将float值设置为标签并将其设置为hidden并将其传递给我的警报,但我确定这不能成为执行此操作的正确方法,一些建议会不胜感激

float x = ([_continuityRingFinalR1.text floatValue]); /stringWithFormat:@"%.1f", x * y]]
float y = (1.67);

if ([_continuityRingFinalRn.text floatValue] > x * y ) {


         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Advisory Warning"
         message:[NSString stringWithFormat: @"Based on your value of %@  this value may not be acceptable. %@ would be acceptable",_continuityRingFinalR1.text, ]///<<< my float value here
         delegate:self cancelButtonTitle: @"Ignore" otherButtonTitles: @"Retest", nil];



    [alert show];
}

2 个答案:

答案 0 :(得分:2)

%f 在NSString中用于float / double而不是%@

    float x = ([_continuityRingFinalR1.text floatValue]); /stringWithFormat:@"%.1f", x * y]]
    float y = (1.67);
    float example;

    if ([_continuityRingFinalRn.text floatValue] > x * y ) {

             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Advisory Warning"
             message:[NSString stringWithFormat: @"Based on your value of %@  this value may not be acceptable. %f would be acceptable",_continuityRingFinalR1.text, example];
             delegate:self cancelButtonTitle: @"Ignore" otherButtonTitles: @"Retest", nil];

        [alert show];
    }

这是一个有用的link

答案 1 :(得分:1)

我想你想要这个

message:[NSString stringWithFormat: @"Based on your value of %@  this value may not be acceptable. %0.2f would be acceptable",_continuityRingFinalR1.text, x*y ];
//Passing second argument as x*y