Search This Blog

Showing posts with label Format Strings. Show all posts
Showing posts with label Format Strings. Show all posts

Sunday, August 26, 2018

Preferred way to format fractional numbers in C#

double myDouble1 = 0.32;
double myDouble2 = 321.5235;

var currentCulture = CultureInfo.InvariantCulture.Clone() as CultureInfo;
var numberFormat = currentCulture.NumberFormat;
numberFormat.NumberDecimalDigits = 2;

Console.WriteLine(myDouble1.ToString("F", numberFormat));
Console.WriteLine(myDouble2.ToString("F", numberFormat));