EN
C# / .NET - round List elements to two decimal places
0
points
In this article, we would like to show you how to round List elements to two decimal places in C#.
Quick solution:
List<double> myList = new List<double> { 1.234, 1.2345, 1.23456 };
myList = myList.Select(x => Math.Round(x, 2)).ToList();
Practical example
In this example, we use Math.Round() to round numbers to two decimal places.
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
List<double> myList = new List<double> { 1, 1.2, 1.23, 1.234, 1.2345 };
myList = myList.Select(x => Math.Round(x, 2)).ToList();
// display result
myList.ForEach(x => Console.WriteLine(x));
}
}
Output:
1
1.2
1.23
1.23
1.23