Convert average Blood glucose values to estimated HbA1c and GMI

CSharp · Console

/Program.cs

using System;

public class Program
{
	const decimal ratio = 0.0555m;
	const int points=1;
	public static void Main()
	{
		Console.WriteLine("Enter your Average Blood Glucose Levels : ");
		string input = Console.ReadLine();
		decimal average = 0m;
		if (!Decimal.TryParse(input, out average))
			throw new Exception("Invalid input");
		Console.WriteLine();
		decimal averageMgdl = 0m;
		decimal averageMmol = 0m;
		if (average > 40m)
		{
			averageMgdl = average;
			averageMmol = Math.Round(average * ratio, points);
			Console.WriteLine($"Assuming units are measured in mg/dl ie {averageMgdl} mg/dl (or {averageMmol} mmol/l)");
		}
		else
		{
			averageMgdl = Math.Round(average / ratio,points);
			averageMmol = average;
			Console.WriteLine($"Assuming units are measured in mmol/l ie {averageMmol} mmol/l (or {averageMgdl} mg/dl)");
		}

		Console.WriteLine();
		var hbA1c_Perc = Math.Round(((averageMgdl + 46.7m) / 28.7m), points);
		var hbA1c_Value = Math.Round(((hbA1c_Perc - 2.15m) * 10.929m), points);
		var gmi_Perc = Math.Round(3.31m + (0.02392m * averageMgdl), points);
		var gmi_Value = Math.Round(12.71m + 4.70587m * averageMmol, points);
		;
		Console.WriteLine($"Your estimated HbA1c is {hbA1c_Value} mmol/mol or {hbA1c_Perc} %");
		Console.WriteLine($"Your estimated GMI   is {gmi_Value} mmol/mol or {gmi_Perc} %");
	}
}
;