CSharp examples for System:Math Statistics
Calculates the median
// Permission is hereby granted, free of charge, to any person obtaining a copy using System.Linq; using System.Collections.Generic; using System;//from www . j ava 2 s.c om public class Main{ /// <summary> /// Calculates the median /// </summary> /// <param name="numbers">Double-values</param> /// <returns>Median</returns> public static double Median(List<double> numbers) { if (numbers != null && numbers.Count > 0) { if (numbers.Count() % 2 == 0) { return numbers.OrderBy(_ => _).Skip(numbers.Count() / 2 - 1).Take(2).Sum() / 2; } return numbers.OrderBy(_ => _).ElementAt(Convert.ToInt32(Math.Floor((Convert.ToDouble(numbers.Count()) / 2)))); } throw new ArgumentException("Numbers are null or 0", "numbers"); } }