BitBindle.ExcelNumberFormat
1.3.1
See the version list below for details.
dotnet add package BitBindle.ExcelNumberFormat --version 1.3.1
NuGet\Install-Package BitBindle.ExcelNumberFormat -Version 1.3.1
<PackageReference Include="BitBindle.ExcelNumberFormat" Version="1.3.1" />
<PackageVersion Include="BitBindle.ExcelNumberFormat" Version="1.3.1" />
<PackageReference Include="BitBindle.ExcelNumberFormat" />
paket add BitBindle.ExcelNumberFormat --version 1.3.1
#r "nuget: BitBindle.ExcelNumberFormat, 1.3.1"
#:package BitBindle.ExcelNumberFormat@1.3.1
#addin nuget:?package=BitBindle.ExcelNumberFormat&version=1.3.1
#tool nuget:?package=BitBindle.ExcelNumberFormat&version=1.3.1
ExcelNumberFormat
.NET library to parse ECMA-376 number format strings and format values like Excel and other spreadsheet softwares.
Install via NuGet
This repository is a fork of ExcelNumberFormat by andersnm. The fork has its own releases, issues and changes. The license is MIT (see LICENSE).
The BitBindle.ExcelNumberFormat package is on nuget.org. To install it, run this command:
dotnet add package BitBindle.ExcelNumberFormat
The package ID differs from the ID of the original package (ExcelNumberFormat). The assembly name and the namespace stay ExcelNumberFormat. A project cannot reference both packages, because they contain the same assembly name.
Usage
var format = new NumberFormat("#.##");
Console.WriteLine(format.Format(1234.56, CultureInfo.InvariantCulture));
A locale code such as [$-407] selects the culture of the month names, day names and AM/PM designators. The library looks up this culture by its id or tag. An application can give the culture itself, for example when it runs with invariant globalization, with a reduced ICU data set, or on netstandard1.0. The resolver gets the low 16 bits of the locale code, or for a language tag such as [$-de-DE] the id 0 and the tag, and the culture of the call. If it returns null, the library looks up the culture. To keep the names of the call for a code that it does not know, it returns the culture of the call:
var format = new NumberFormat("[$-407]d. mmmm yyyy");
Console.WriteLine(format.Format(44995, CultureInfo.InvariantCulture, false, (localeId, localeName, culture) => localeId == 0x407 ? myGermanCulture : culture));
NumberFormat.DefaultLocaleCultureResolver sets a resolver for all calls in the process, also in libraries that call Format(value, culture):
NumberFormat.DefaultLocaleCultureResolver = (localeId, localeName, culture) => localeId == 0x407 ? myGermanCulture : culture;
Features
- Parses and formats most custom number formats as expected: decimal, percent, thousands, exponential, fraction, currency, date/time, duration, text.
- Supports multiple sections with conditions.
GetColor(value)gives the color of the section that formats the value, for example[Red]or[Color10]: a color name and a palette index from 1 to 56, or null.- Describes a format without a value:
IsDateTimeFormat,IsTimeSpanFormat,IsPercentFormat(a section has a%outside quotes), andDateTimeParts(the year, month, day, hour, minute and second codes of the first section that shows a date, a time or an elapsed time). - Formats values with relevant constants from CultureInfo.
- A locale code such as
[$-407]selects the language of the month names, day names and AM/PM designators. A language tag, such as[$-de-DE], has the same effect. Excel also writes a tag, such as[$-en-US,1], in the formatCode16 attribute. numfmt also reads POSIX names, such as[$-de_DE.utf8]or[$-de@euro], and the library reads them as tags. A code such as[$-de]is a locale id and a tag; the tag gives the culture if the id does not. Only[$-411]selects the Japanese era.[$-x-sysdate]and[$-x-systime]stay unknown codes. The separators always come from the CultureInfo. If the language of the locale code is not known, or for a locale id on netstandard1.0, the names also come from the CultureInfo. On netstandard1.0, an unknown tag can give the names of the invariant culture. - Supports DateTime, TimeSpan and numeric values for date and duration formats.
- Supports both 1900- and 1904-based numeric datetimes (Excel on Mac uses 1904-based dates).
- Targets net20 and netstandard1.0 for max compatibility.
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 Kind | Example | .NET type | Conversion strategy |
|---|---|---|---|
| Number | 0.00 | double | Convert.ToDouble() |
| Fraction | 0/0 | double | Convert.ToDouble() |
| Exponent | #0.0E+0 | double | Convert.ToDouble() |
| Date/Time | hh:mm | DateTime | ExcelDateTime.TryConvert() |
| Duration | [hh]:mm | TimeSpan | Cast or TimeSpan.FromDays() |
| General | General | (any) | Excel General rules for numbers, else CompatibleConvert.ToString() |
| Text | ;;;"Text: "@ | string | Convert.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
- 'General' ignores the column width. Numbers show at most 11 characters.
- No errors: Invalid format strings and incompatible input values are formatted with
.ToString(). - Variable width space is returned as regular space.
- Repeat-to-fill characters are printed once, not repeated.
- No alignment hinting.
- No date conditions.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp1.0 was computed. netcoreapp1.1 was computed. netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard1.0 is compatible. netstandard1.1 was computed. netstandard1.2 was computed. netstandard1.3 was computed. netstandard1.4 was computed. netstandard1.5 was computed. netstandard1.6 was computed. netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net20 is compatible. net35 was computed. net40 was computed. net403 was computed. net45 was computed. net451 was computed. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen30 was computed. tizen40 was computed. tizen60 was computed. |
| Universal Windows Platform | uap was computed. uap10.0 was computed. |
| Windows Phone | wp8 was computed. wp81 was computed. wpa81 was computed. |
| Windows Store | netcore was computed. netcore45 was computed. netcore451 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 2.0
- No dependencies.
-
.NETStandard 1.0
- NETStandard.Library (>= 1.6.1)
-
.NETStandard 2.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on BitBindle.ExcelNumberFormat:
| Package | Downloads |
|---|---|
|
BitBindle.ClosedXML
See release notes https://github.com/BitBindle/ClosedXML/releases/tag/0.107.7 ClosedXML is a .NET library that reads, changes and writes Excel 2007+ files (.xlsx, .xlsm). It gives an easy interface to the Open XML SDK. |
GitHub repositories
This package is not used by any popular GitHub repositories.