Project · Beginner
Temperature table
Keep conversion logic separate from invariant table formatting.
All C# source files below are loaded together in the playground. Files written by a run live in its virtual directory.
Program.cs
using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
foreach (var celsius in new[] { -10m, 0m, 20m, 100m })
{
var fahrenheit = Temperature.ToFahrenheit(celsius);
Console.WriteLine(celsius.ToString("0.0", CultureInfo.InvariantCulture) + " C = " + fahrenheit.ToString("0.0", CultureInfo.InvariantCulture) + " F");
}
Math/Temperature.cs
public static class Temperature
{
public static decimal ToFahrenheit(decimal celsius) => celsius * 9m / 5m + 32m;
public static decimal ToCelsius(decimal fahrenheit) => (fahrenheit - 32m) * 5m / 9m;
}
Temperaturetable.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>