簡體   English   中英

如何在iPhone SDK中傳遞float數據類型中的值?

[英]How can i pass the value in float data type in iPhone sdk?

這是我的字符串,我想在此字符串中放置變量浮點數:

我想以float的數據類型傳遞值。

例如..%。1f%.2f,我想在iPhone SDK中動態傳遞1和2值,我使用此代碼:-----

NSString *string=[NSString stringWithFormat:@"%.%if",[[arrayvalue objectAtIndex:0] intValue],[textField2.text floatValue]];

但它僅打印:-.if

您不能嵌套格式字符串,可以這樣做:

int value = [[arrayvalue objectAtIndex:0] intValue];
NSString *string = [@"%." stringByAppendingFormat:@"%if", value];
string = [NSString stringWithFormat:string, [textField2.text floatValue]];

這是完全未經測試的代碼。

您忘記為此符號添加“%”的轉義字符。 試試下面的代碼,它將起作用。

int i=2;
NSString *output= [NSString stringWithFormat:@"%%.%if",i];

如果要顯示兩個值,則可以嘗試以下操作。

int i=2;
float x=3.5;
NSString *output= [NSString stringWithFormat:@"%%.%if  %%.%ff",i,x];

請根據需要進行修改。 這只是您的想法如何使用它。

NSMutableString可以添加其他字符串或格式。

// array must have first object pass 1st parameter
NSMutableString *mutableString = [[NSString alloc] init];
[str appendFormat:"%d", [array objectAtIndex:0].intValue];

// array have second object then pass 2nd parameter
if (2 <= array.count)
   [mutableString appendFormat:"%f", [array objectAtIndex:0].floatValue]];

NSLog("%@", mutableString);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM