Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在这个方法中去掉小数_Ios_Xcode_Nsstring - Fatal编程技术网

Ios 在这个方法中去掉小数

Ios 在这个方法中去掉小数,ios,xcode,nsstring,Ios,Xcode,Nsstring,我有一份声明显示50.02% 我怎样才能去掉小数并显示50% 很抱歉,这太基本了,对xcode来说还很新 先谢谢你 -(void)updatePercent{ percent.text = [NSString stringWithFormat:@"%.2f%@", 100.0f, @"%"]; }使用以下方法: percent.text = [NSString stringWithFormat:@"%.0f%%", 100.0f]; 还要注意使用%%打印单个%%使用以下方法: perce

我有一份声明显示50.02%

我怎样才能去掉小数并显示50%

很抱歉,这太基本了,对xcode来说还很新

先谢谢你

 -(void)updatePercent{

percent.text = [NSString stringWithFormat:@"%.2f%@", 100.0f, @"%"];
}

使用以下方法:

percent.text = [NSString stringWithFormat:@"%.0f%%", 100.0f];
还要注意使用
%%
打印单个
%%

使用以下方法:

percent.text = [NSString stringWithFormat:@"%.0f%%", 100.0f];

另请注意使用
%%
打印单个
%%

打印浮动时,您可以使用
%f
格式语句中
%%
f
之间的数字来控制打印出的小数位数

小数点右边的数字表示应打印多少位小数。小数点左边的数字表示至少应打印多少位(包括小数点和所有数字)。如果左侧的数字以零作为前缀,则生成的字符串将在左侧追加零

float myNum = 32.142;

// without declaring total digits
[NSString stringWithFormat:@"%.0f"]  // 32
[NSString stringWithFormat:@"%.2f"]  // 32.14
[NSString stringWithFormat:@"%.5f"]  // 32.14200

// put a zero in front of the left digit
[NSString stringWithFormat:@"%010.0f"] // 0000000032
[NSString stringWithFormat:@"%010.2%"] // 0000032.14

// without the zero prefix (leading whitespace)
[NSString stringWithFormat:@"%10.2f"]  //      32.14

打印浮点时,您可以使用
%f
格式语句中
%
f
之间的数字来控制打印出的小数位数

小数点右边的数字表示应打印多少位小数。小数点左边的数字表示至少应打印多少位(包括小数点和所有数字)。如果左侧的数字以零作为前缀,则生成的字符串将在左侧追加零

float myNum = 32.142;

// without declaring total digits
[NSString stringWithFormat:@"%.0f"]  // 32
[NSString stringWithFormat:@"%.2f"]  // 32.14
[NSString stringWithFormat:@"%.5f"]  // 32.14200

// put a zero in front of the left digit
[NSString stringWithFormat:@"%010.0f"] // 0000000032
[NSString stringWithFormat:@"%010.2%"] // 0000032.14

// without the zero prefix (leading whitespace)
[NSString stringWithFormat:@"%10.2f"]  //      32.14

格式字符串实际上应该是
@“%.0f%%”
。格式字符串实际上应该是
@“%.0f%%”