Table of Contents

Class InvariantNumber

Namespace
PluginUI
Assembly
PluginUI.dll

Culture-independent number formatting and parsing used for every number PluginUI displays or reads from the user: the decimal separator is always a point (1.5), whatever the language of the interface (Language) and the regional settings of the machine (CurrentCulture).

public static class InvariantNumber
Inheritance
InvariantNumber
Inherited Members

Examples

string text = InvariantNumber.Format(1234.5, "0.0");   // "1234.5" on every machine
bool ok = InvariantNumber.TryParse(" -0.25 ", out double v); // true, v = -0.25
bool bad = InvariantNumber.TryParse("0,25", out _);         // false: comma rejected

Remarks

Accepted input: optional sign, digits, an optional point and fraction, an optional exponent (-12, 0.5, .5, 1e3), surrounded by optional spaces. A comma is rejected rather than guessed: 1,5 could mean one and a half or fifteen depending on the reader, so the text box reports the value as invalid instead of applying a wrong one. Thousands separators are not accepted either.

Use these helpers in custom controls and formatters instead of Parse(string) or ToString(), which follow the regional settings.

Properties

Culture

The culture used for all numbers: InvariantCulture.

public static CultureInfo Culture { get; }

Property Value

CultureInfo

Methods

Format(double, int)

Formats a number with a fixed count of decimals and a decimal point.

public static string Format(double value, int decimals)

Parameters

value double

The value to format.

decimals int

Number of decimals (clamped to 0..15).

Returns

string

The formatted text, e.g. "12.30" for Format(12.3, 2).

Format(double, string?)

Formats a number with a decimal point.

public static string Format(double value, string? format = null)

Parameters

value double

The value to format.

format string

A standard or custom .NET numeric format string ("F2", "0.##"…); null = shortest round-trip form.

Returns

string

The formatted text, e.g. "0.50" for Format(0.5, "F2").

TryParse(ReadOnlySpan<char>, out double)

Parses a number written with a decimal point.

public static bool TryParse(ReadOnlySpan<char> text, out double value)

Parameters

text ReadOnlySpan<char>

The text to parse (may be null).

value double

The parsed value, or 0 when the text is not a valid number.

Returns

bool

true if text is a finite number in the accepted syntax (see remarks of InvariantNumber).

TryParse(string?, out double)

Parses a number written with a decimal point.

public static bool TryParse(string? text, out double value)

Parameters

text string

The text to parse (may be null).

value double

The parsed value, or 0 when the text is not a valid number.

Returns

bool

true if text is a valid finite number.