我有一些集合返回的字段为
2.4200
2.0044
2.0000
我想得到的结果
2.42
2.0044
2
我尝试过String.Format,但它返回2.0000并将其设置为N0舍入其他值.
是否有一个显示格式化程序,它将在c#中输出小数作为这些字符串表示而不进行任何舍入?
// decimal -> string
20 -> 20
20.00 -> 20
20.5 -> 20.5
20.5000 -> 20.5
20.125 -> 20.125
20.12500 -> 20.125
0.000 -> 0
{0.#}将舍入,并且使用某些Trim类型函数将无法使用网格中的绑定数字列.
从XML文件中我收到格式的小数:
1.132000
6.000000
目前我正在使用Decimal.Parse,如下所示:
decimal myDecimal = Decimal.Parse(node.Element("myElementName").Value, System.Globalization.CultureInfo.InvariantCulture);
如何将myDecimal打印到字符串中,如下所示?
1.132
6
以下代码目前输出:
12.1
12.100
12.1000
12.00
12
12.0000
如何更改它以便输出:
12.1
12.1
12.1
12
12
12
Math.Round似乎是事情,但它让我定义了我想要的小数位数,但我希望它们如上所述变量.
如果没有数学方法可以做到这一点,我只会从字符串右侧删除零和小数点,但会认为有一种数学方法来处理它.
using System;
using System.Collections.Generic;
namespace Test8834234
{
    public class Program
    {
        static void Main(string[] args)
        {
            List<string> decimalsAsStrings = new List<string>
            {
                "12.1",
                "12.100",
                "12.1000",
                "12.00",
                "12",
                "12.0000"
            };
            foreach (var decimalAsString in decimalsAsStrings)
            {
                decimal dec = decimal.Parse(decimalAsString);
                Console.WriteLine(dec);
            }
            Console.ReadLine();
        }
    }
}