Home

Awesome

ExcelNumberFormat

.NET library to parse ECMA-376 number format strings and format values like Excel and other spreadsheet softwares.

Build status

Install via NuGet

If you want to include ExcelNumberFormat in your project, you can install it directly from NuGet

To install ExcelNumberFormat, run the following command in the Package Manager Console

PM> Install-Package ExcelNumberFormat

Usage

var format = new NumberFormat("#.##");
Console.WriteLine(format.Format(1234.56, CultureInfo.InvariantCulture));

Features

Formatting .NET types

The Format() method takes a value of type object as parameter. Internally, the value is cast or converted to a specific .NET type depending on the kind of number format:

Format KindExample.NET typeConversion strategy
Number0.00doubleConvert.ToDouble()
Fraction0/0doubleConvert.ToDouble()
Exponent#0.0E+0doubleConvert.ToDouble()
Date/Timehh:mmDateTimeExcelDateTime.TryConvert()
Duration[hh]:mmTimeSpanCast or TimeSpan.FromDays()
GeneralGeneral(any)CompatibleConvert.ToString()
Text;;;"Text: "@stringConvert.ToString()

In case of errors, Format() returns the value from CompatibleConvert.ToString().

CompatibleConvert.ToString() formats floats and doubles with explicit precision, or falls back to Convert.ToString() for any other types. ExcelDateTime.TryConvert() uses DateTimes as is, or converts numeric values to a DateTime with adjustments for legacy Excel behaviors.

TODO/notes