字符串格式 - 如何改变负号位置 [英] string Format - how to change negative sign position

查看:135
本文介绍了字符串格式 - 如何改变负号位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的String.Format 是这样的:

string Test = string.Format("{0:#,0}", NegativeNumber);



我怎样才能改变负号位置(方向 - >左或右)?

how can I change the negative sign position (Direction -> left or right)?

推荐答案

,最快的途径可能是只是有负数的不同格式

The easiest route might be to just have a different format for negative numbers

string Test = string.Format("{0:#,0;#,0-}", NegativeNumber);



结果:

Results:

PS C:\> '{0:#,0;#,0-}' -f -17.2

17-

PS C:\> '{0:#,0;#,0-}' -f 17.2

17

自定义数字格式字符串

分号(;)是不同的格式,具体取决于是否多项应用条件格式说明其值是正,负或零。产生这种行为,自定义格式字符串最多可以包含用分号分隔的三个部分。这些部分的下表中描述。

The semicolon (;) is a conditional format specifier that applies different formatting to a number depending on whether its value is positive, negative, or zero. To produce this behavior, a custom format string can contain up to three sections separated by semicolons. These sections are described in the following table.

这篇关于字符串格式 - 如何改变负号位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆