C# decimal take ceiling 2

后端 未结 4 1555
半阙折子戏
半阙折子戏 2020-12-02 01:00

I want to round my decimal value like 2.2222 to 2.23. When I use round,

decimal a = Math.Round((decimal)2.222, 2);

When I use ceiling, it c

相关标签:
4条回答
  • 2020-12-02 01:20
    decimal c = Math.Ceiling((decimal)2.2222*100)/100;
    

    but it's stupid.

    0 讨论(0)
  • 2020-12-02 01:35
    public static decimal CeilingAfterPoint(this decimal number, int digitsAfterPoint) {
        return Math.Ceiling(number * (decimal)Math.Pow(10, digitsAfterPoint))
               / (decimal)Math.Pow(10, digitsAfterPoint);
    }
    
    0 讨论(0)
  • 2020-12-02 01:36

    try something like

    decimal c = Math.Ceiling((decimal)2.222*100)/100;
    

    but it fails if your value is 2.22

    0 讨论(0)
  • 2020-12-02 01:41

    I solved my problem..

     string n = "2.2222";
            string[] s = n.Split('.');
    
            if (s[1].Count() >= 3)
            {
                List<char> z = s[1].ToString().Take(2).ToList();
                int c=Convert.ToInt32(z[0].ToString() + z[1].ToString()) + 1;
              //  int b = Convert.ToInt32(s[1].ElementAt(0).ToString() + s[1].ElementAt(1).ToString()) + 1;
                string output= s[0] + "." + c.ToString();            
            }
    

    now any number can put ,it will take 2 decimal value and add 1.Thanks.

    0 讨论(0)
提交回复
热议问题