有没有办法格式化小数,使其显示为带有+或 - 符号的货币?
例如:
+ $ 5.00(大于零的加号)
$ 0.00(零没有符号)
- $ 5.00(减号小于零)
以下是我想要的但不确定如何合并货币:
var formattedprice = $"{price:+0;-#}"
Run Code Online (Sandbox Code Playgroud)
我通常使用C0作为货币或N0作为数字.
class Program
{
static void Main(string[] args)
{
var pos = 5m;
var zero = 0m;
var neg = -5m;
var format = "+$0.00;-$0.00;$0.00";
Console.WriteLine(pos.ToString(format));
Console.WriteLine(zero.ToString(format));
Console.WriteLine(neg.ToString(format));
}
}
Run Code Online (Sandbox Code Playgroud)
而输出是
+$5,00
$0,00
-$5,00
Run Code Online (Sandbox Code Playgroud)
根据@ xxbbcc的评论,货币符号位置取决于区域设置,因此您必须按照更改格式.
| 归档时间: |
|
| 查看次数: |
1019 次 |
| 最近记录: |