如何在字符串格式中添加百分号? [重复]

时间:2022-09-15 08:47:53

This question already has an answer here:

这个问题在这里已有答案:

I want to get string like "99.99%" from Double,and I used format to get this:

我想从Double获得类似“99.99%”的字符串,我用格式来得到这个:

let rate = 99.99999
let str = String(format: "%.2f%", rate)
// output 99.99

And \% is not allowed. So how to add percent sign in string format, please help me!

并且\%是不允许的。那么如何在字符串格式中添加百分号,请帮帮我!

2 个解决方案

#1


33  

write two time %:

写两次%:

rate = 99.99
let str = String(format: "%.2f%%", rate)

#2


4  

The % symbol has a special use in a printf statement. How would you place this character as part of the output on the screen?

%符号在printf语句中有特殊用途。你如何将这个角色作为屏幕输出的一部分?

You can do this by using %% in the printf statement. For example, you can write printf(“10%%”) to have the output appear as 10% on the screen.

您可以在printf语句中使用%%来完成此操作。例如,您可以编写printf(“10 %%”)以使输出在屏幕上显示为10%。

Hope it help you :)

希望它能帮到你:)

#1


33  

write two time %:

写两次%:

rate = 99.99
let str = String(format: "%.2f%%", rate)

#2


4  

The % symbol has a special use in a printf statement. How would you place this character as part of the output on the screen?

%符号在printf语句中有特殊用途。你如何将这个角色作为屏幕输出的一部分?

You can do this by using %% in the printf statement. For example, you can write printf(“10%%”) to have the output appear as 10% on the screen.

您可以在printf语句中使用%%来完成此操作。例如,您可以编写printf(“10 %%”)以使输出在屏幕上显示为10%。

Hope it help you :)

希望它能帮到你:)