Rounding of nearest 0.5

后端 未结 7 1083
灰色年华
灰色年华 2021-01-14 16:03

I want to be rounded off this way

13.1, round to 13.5
13.2, round to 13.5
13.3, round to 13.5
13.4, round to 13.5
13.5 = 13.5
13.6, round to 14.0
13.7, roun         


        
相关标签:
7条回答
  • 2021-01-14 16:30

    I don't know if it is proper way, but it works. Try this if you want:

            double doubleValue = 13.5;
            double roundedValue = 0.0;
            if (doubleValue.ToString().Contains('.'))
            {
                string s = doubleValue.ToString().Substring(doubleValue.ToString().IndexOf('.') + 1);
                if (Convert.ToInt32(s) == 5)
                {
                    roundedValue = doubleValue;
                }
                else
                {
                    roundedValue = Math.Round(doubleValue);
                }
            }
    
            Console.WriteLine("Result:      {0}", roundedValue);
    
    0 讨论(0)
提交回复
热议问题