格式化双内标签文本的十进制数字

时间:2016-05-25 11:34:04

标签: c# string-formatting

我有这行代码:

label4.Text = "(" + x1 + ", 0), (" + x2 + ",0)";

x1 x2 都是双值。我无法用两位小数显示它们。我知道我可以使用{0:N2}但我找不到正确的语法方式来使用它因为我想要显示的字符串中有两个值。如何编写它以使两个值( x1 x2 )显示在只有2位十进制数字的字符串中?

3 个答案:

答案 0 :(得分:4)

使用string.Format,您可以在格式字符串中创建标记,然后提供匹配数量的参数来填充标记:

// Creating a connection
XMPPTCPConnectionConfiguration connConfig =
        XMPPTCPConnectionConfiguration.builder()
                .setHost("myHost.com")  // Name of your Host
                .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                .setPort(5222)          // Your Port for accepting c2s connection
                .setDebuggerEnabled(true)
                .setServiceName("myServiceName")
                .build();
AbstractXMPPConnection connection = new XMPPTCPConnection(connConfig);

try {
    // connecting...
    connection.connect();
    Log.i("TAG", "Connected to " + connection.getHost());

    // Registering the user
    AccountManager accountManager = AccountManager.getInstance(connection);
    accountManager.sensitiveOperationOverInsecureConnection(true);
    accountManager.createAccount(username, password);   // Skipping optional fields like email, first name, last name, etc..
} catch (SmackException | IOException | XMPPException e) {
    Log.e("TAG", e.getMessage());
    xmppClient.setConnection(null);
}

答案 1 :(得分:2)

试试我的朋友:

double x1 = 10.5;
double x2 = 5.666;

label4.Text = String.Format("({0:N2}), ({1:N2})", x1, x2);

此表达式的结果:

 "(10.50), (5.67)"

答案 2 :(得分:1)

你可以这样做:

function template_redirect_fn()
{
    if(is_page (Page ID, title, slug of account page ) && !is_user_logged_in ())
    {
        $loginUrl = home_url('/login-page/');
        wp_redirect($loginUrl);
         exit(); 
    }

}
add_action( 'template_redirect', 'template_redirect_fn' );

或在C#6中使用string interpolation

label4.Text = string.Format("({0:0.00}, 0), ({1:0.00},0)", x1, x2);