相关疑难解决方法(0)

删除尾随零

我有一些集合返回的字段为

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舍入其他值.

.net c# decimal

160
推荐指数
7
解决办法
13万
查看次数

有没有办法获得小数的"有效数字"?

更新

好的,经过一番调查,并且在很大程度上要归功于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)

.net precision decimal significant-digits

17
推荐指数
1
解决办法
3867
查看次数

解析十进制并在右边过滤额外的0?

从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)

c# string decimal

15
推荐指数
2
解决办法
7123
查看次数

标签 统计

decimal ×3

.net ×2

c# ×2

precision ×1

significant-digits ×1

string ×1