我有一些集合返回的字段为
2.4200
2.0044
2.0000
Run Code Online (Sandbox Code Playgroud)
我想得到的结果
2.42
2.0044
2
Run Code Online (Sandbox Code Playgroud)
我尝试过String.Format,但它返回2.0000并将其设置为N0舍入其他值.
好的,经过一番调查,并且在很大程度上要归功于Jon和Hans提供的有用答案,这就是我能够把它放在一起的.到目前为止,我认为它似乎运作良好.当然,我不打赌我的生活完全正确.
public static int GetSignificantDigitCount(this decimal value)
{
/* So, the decimal type is basically represented as a fraction of two
* integers: a numerator that can be anything, and a denominator that is
* some power of 10.
*
* For example, the following numbers are represented by
* the corresponding fractions:
*
* VALUE NUMERATOR DENOMINATOR
* 1 1 1
* 1.0 10 10
* 1.012 1012 1000
* 0.04 4 100
* 12.01 1201 100
* …Run Code Online (Sandbox Code Playgroud) 从XML文件中我收到格式的小数:
1.132000
6.000000
Run Code Online (Sandbox Code Playgroud)
目前我正在使用Decimal.Parse,如下所示:
decimal myDecimal = Decimal.Parse(node.Element("myElementName").Value, System.Globalization.CultureInfo.InvariantCulture);
Run Code Online (Sandbox Code Playgroud)
如何将myDecimal打印到字符串中,如下所示?
1.132
6
Run Code Online (Sandbox Code Playgroud)