@pbkware/dot-net-date-number-formatting - v0.3.2
    Preparing search index...

    Class DotNetFloatFormatter

    Formatter for floating-point numbers using .NET-compatible format strings.

    Supports all standard numeric format strings (C, D, E, F, G, N, P, R, X, B) and custom format strings with digit placeholders, separators, and sections.

    const formatter = new DotNetFloatFormatter();
    formatter.localeSettings = DotNetLocaleSettings.createInvariant();

    // Currency format
    formatter.trySetFormat('C2');
    console.log(formatter.toString(1234.56)); // "$1,234.56"

    // Percentage format
    formatter.trySetFormat('P1');
    console.log(formatter.toString(0.1234)); // "12.3%"

    // Custom format with sections
    formatter.trySetFormat('#,##0.00;(#,##0.00)');
    console.log(formatter.toString(-1234.56)); // "(1,234.56)"

    // Parsing
    formatter.styles = DotNetNumberStyles.number;
    const result = formatter.tryFromString('1,234.56');
    if (result.isOk()) {
    console.log(result.value); // 1234.56
    }

    Hierarchy (View Summary)

    Index
    format: string = ""
    localeSettings: DotNetLocaleSettings = DotNetLocaleSettings.current

    The locale settings that determine decimal/thousands separators and other culture-specific formatting.

    parseErrorText: string = ""

    Contains the error message from the last failed operation. Check this property if DotNetNumberFormatter.trySetFormat or parsing methods return an error.

    styles: number = DotNetNumberStyles.number

    The (DotNetNumberStyles:var) that control which number formats are allowed during parsing.

    formatter.styles = DotNetNumberStyles.allowLeadingSign | DotNetNumberStyles.allowDecimalPoint;
    
    • Formats a number using the current format string.

      Parameters

      • value: number

        The number to format.

      Returns string

      The formatted number string.

    • Attempts to parse a number from a string using the current parsing styles.

      Parameters

      • strValue: string

        The string to parse.

      Returns Result<number>

      A Result containing the parsed number if successful, or an error if parsing fails.

      formatter.styles = DotNetNumberStyles.number;
      const result = formatter.tryFromString(' 1,234.56 ');
      if (result.isOk()) {
      console.log(result.value); // 1234.56
      }
    • Sets the format string to use for formatting numbers.

      The format string is tokenized to confirm validity and to optimize formatting/parsing operations.

      Parameters

      • value: string

        A standard format string (e.g., "C", "N2", "E3") or custom format string (e.g., "#,##0.00").

      Returns Result<void>

      A Result indicating success or containing an error message if the format string is invalid.

      // Standard format
      formatter.trySetFormat('C2'); // Currency with 2 decimal places

      // Custom format
      formatter.trySetFormat('#,##0.00'); // Number with thousands separator

      // Check for errors
      const result = formatter.trySetFormat('INVALID');
      if (result.isErr()) {
      console.error(result.error);
      }