CSharp · Console
using System;
public class TwoSectionsFormattingTest // see https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings#the--section-separator
{
private const string SimpleFormat = "p#;n#";
private const string ZeroFormat = "p0#;n0#";
private const string OneFormat = "p1#;n1#";
private const string ZeroPointFormat = "p0.#;n0.#";
private const string OnePointFormat = "p1.#;n1.#";
private static readonly string printFormat = "{0,-40}{1}";
private const string LhsFormat = @"({0}).ToString(""{1}""):";
static void Print(int number)
{
Console.WriteLine();
Console.WriteLine(printFormat, string.Format(LhsFormat, number, SimpleFormat), number.ToString(SimpleFormat));
Console.WriteLine(printFormat, string.Format(LhsFormat, number, ZeroFormat), number.ToString(ZeroFormat));
Console.WriteLine(printFormat, string.Format(LhsFormat, number, OneFormat), number.ToString(OneFormat));
Console.WriteLine(printFormat, string.Format(LhsFormat, number, ZeroPointFormat), number.ToString(ZeroPointFormat));
Console.WriteLine(printFormat, string.Format(LhsFormat, number, OnePointFormat), number.ToString(OnePointFormat));
}
static void Print(double number)
{
Console.WriteLine();
Console.WriteLine(printFormat, string.Format(LhsFormat, number, SimpleFormat), number.ToString(SimpleFormat));
Console.WriteLine(printFormat, string.Format(LhsFormat, number, ZeroFormat), number.ToString(ZeroFormat));
Console.WriteLine(printFormat, string.Format(LhsFormat, number, OneFormat), number.ToString(OneFormat));
Console.WriteLine(printFormat, string.Format(LhsFormat, number, ZeroPointFormat), number.ToString(ZeroPointFormat));
Console.WriteLine(printFormat, string.Format(LhsFormat, number, OnePointFormat), number.ToString(OnePointFormat));
}
public static void Main()
{
Print(-1.1);
Print(-1);
Print(-0.1);
Print(-0.01);
Print(0);
Print(+0);
Print(0.01);
Print(0.1);
Print(1);
Print(1.1);
}
}