[英]How to assign a “% symbol” to decimal value - so that i can export the same in Excel sheet
How to assign a "% Symbol" to decimal value in C#. 如何在C#中为十进制值分配“%符号”。 Here is my code.
这是我的代码。
So my question is, is there any way I can assign a "%" symbol to BRCList[l].TotalDifference.Value
. 所以我的问题是,有什么办法可以给
BRCList[l].TotalDifference.Value
分配一个“%”符号。 BRCList[l].TotalDifference.Value
is decimal type from the service side. 从服务端
BRCList[l].TotalDifference.Value
是十进制类型。
I need to append "%" symbol: 我需要附加“%”符号:
string TotalDifference = string.Empty;
if (Type == "P")
{
TotalDifference = String.Format("{0:0.##}", BRCList[l].TotalDifference.Value) + "%";
}
You cannot assign an appended character to a base numeric type. 您不能将附加字符分配给基本数字类型。 The only way would be to first convert it to a string like you do in the code above.
唯一的方法是像上面的代码一样首先将其转换为字符串。
The other option would be to box it in a custom struct that knows it's a percentage stored as a decimal. 另一种选择是将其装在一个自定义结构中,该结构知道它是以百分比形式存储的小数。 Then you could override that struct's ToString method to always automatically append the percentage sign, but you'd also probably need to override all the logical and arithmetic operators that decimal normally supports to maintain any sort of compatibility.
然后,您可以覆盖该结构的ToString方法以始终自动附加百分号,但是您可能还需要覆盖十进制通常支持的所有逻辑和算术运算符,以保持任何兼容性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.