如何在字符串格式中添加百分号?

时间:2016-05-25 06:37:02

标签: ios string swift format

我希望从"99.99%"获取Double之类的字符串,并使用格式来获​​取此内容:

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

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

2 个答案:

答案 0 :(得分:58)

写两次%:

$uri = '//' .$_SERVER[ 'HTTP_HOST' ] . $_SERVER[ 'REQUEST_URI' ]; // Alternative to get_permalink() does not work in all situations. In real life this variable is global.

$arrayHttp = [ 'http://', 'https://' ]; // Here it's not very nice, is to prepare the str_replace that follows ...
$testLink = ( $tabUri == $uri );
foreach ( ( array ) $menu_items as $key => $menu_item ) {
            $title = $menu_item->title;
            $tabUri = $menu_item->url; // It retrieves the URL of the tab
            $tabUri = str_replace( $arrayHttp, '//', $tabUri ); // Treatment allows for comparison with the variable $url. If you have a solution for dealing with HyperText Transfer Protocol in a more elegant way, I'm interested ...
            // Following the foreach...
}

答案 1 :(得分:8)

  

%符号在printf语句中有特殊用途。你好吗?   将此字符作为输出的一部分放在屏幕上?

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

希望它对你有所帮助:)。

相关问题