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

    Class DotNetLocaleSettings

    Represents locale-specific formatting settings for dates, times, and numbers.

    Provides culture-specific separators and formatting options that determine how numbers, currencies, dates, and times are formatted and parsed.

    // Use invariant culture (en-US)
    const invariant = DotNetLocaleSettings.createInvariant();

    // Use specific locale
    const french = DotNetLocaleSettings.create('fr-FR');
    console.log(french.decimalSeparator); // ","
    console.log(french.thousandSeparator); // " "

    // Use current system locale
    const current = DotNetLocaleSettings.current;
    Index
    • Creates a new DotNetLocaleSettings instance.

      Parameters

      • OptionallocaleName: string

        Optional locale name (e.g., "en-US", "fr-FR"). If undefined, uses the system's current locale.

      Returns DotNetLocaleSettings

      // System locale
      const settings = new DotNetLocaleSettings(undefined);

      // Specific locale
      const french = new DotNetLocaleSettings('fr-FR');
    currencyString: string

    Currency symbol for this locale (e.g., "$", "€", "£").

    dateSeparator: string

    Character used between date components (e.g., "/", "-", ".").

    decimalSeparator: string

    Character used for decimal point (e.g., "." or ",").

    defaultCurrency: NumberFormat

    Pre-configured number formatter for currency values.

    defaultDecimal: NumberFormat

    Pre-configured number formatter for high-precision decimal values.

    defaultFloat: NumberFormat

    Pre-configured number formatter for floating-point values.

    id: string

    The locale identifier (same as name).

    name: string

    The locale name (e.g., "en-US", "fr-FR").

    thousandSeparator: string

    Character used for thousands grouping (e.g., "," or "." or " ").

    timeSeparator: string

    Character used between time components (typically ":").

    current: DotNetLocaleSettings = ...

    Singleton instance for current system locale. Uses the locale settings from the runtime environment.

    invariant: DotNetLocaleSettings = ...

    Singleton instance for invariant culture (en-US). Provides consistent formatting across all systems.

    • Converts a boolean to a string.

      Parameters

      • value: boolean

        The boolean value.

      Returns string

      "true" or "false".

      settings.boolToStr(true);   // "true"
      settings.boolToStr(false); // "false"
    • Converts a Date to a string using locale-specific formatting.

      Parameters

      • value: Date

        The Date to format.

      • Optionaloptions: DateTimeFormatOptions

        Optional Intl.DateTimeFormatOptions for custom formatting.

      Returns string

      The formatted date string.

      const settings = DotNetLocaleSettings.create('en-US');
      settings.dateToStr(new Date(2024, 6, 5), { dateStyle: 'short' }); // "7/5/2024"
    • Converts a number or bigint to a string using locale-specific formatting.

      Parameters

      • value: number | bigint

        The number or bigint to format.

      • Optionaloptions: NumberFormatOptions

        Optional Intl.NumberFormatOptions for custom formatting.

      Returns string

      The formatted number string.

      const settings = DotNetLocaleSettings.create('en-US');
      settings.numberToStr(1234.56); // "1,234.56"
      settings.numberToStr(1234.56, { minimumFractionDigits: 2 }); // "1,234.56"
    • Convert a single character to lowercase using culture-specific rules. Handles special cases like Turkish 'I' → 'ı' vs Turkish 'İ' → 'i'.

      Parameters

      • char: string

        The character to convert to lowercase.

      Returns string

      The lowercased character.

      const usSettings = DotNetLocaleSettings.create('en-US');
      usSettings.toLowerChar('I'); // "i"

      const trSettings = DotNetLocaleSettings.create('tr-TR');
      trSettings.toLowerChar('I'); // "ı" (Turkish dotless i)
      trSettings.toLowerChar('İ'); // "i" (Turkish dotted i)
    • Convert a single character to uppercase using culture-specific rules. Handles special cases like Turkish 'i' → 'İ' vs standard 'i' → 'I'.

      Parameters

      • char: string

        The character to convert to uppercase.

      Returns string

      The uppercased character.

      const usSettings = DotNetLocaleSettings.create('en-US');
      usSettings.toUpperChar('i'); // "I"

      const trSettings = DotNetLocaleSettings.create('tr-TR');
      trSettings.toUpperChar('i'); // "İ" (Turkish dotted I)
    • Attempts to parse a BigInt from a string.

      Parameters

      • value: string

        The string to parse. Must contain only digits and an optional leading +/- sign.

      Returns Result<bigint>

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

      settings.tryStrToBigInt('12345678901234567890');  // Ok(12345678901234567890n)
      settings.tryStrToBigInt('-999'); // Ok(-999n)
    • Attempts to parse a boolean value from a string.

      Parameters

      • value: string

        The string to parse. Accepts: "true", "1", "yes" (case-insensitive) for true, and "false", "0", "no" for false.

      Returns Result<boolean>

      A Result containing the boolean value if successful, or an error if parsing fails.

      settings.tryStrToBool('yes');    // Ok(true)
      settings.tryStrToBool('false'); // Ok(false)
      settings.tryStrToBool('invalid'); // Err("Invalid boolean string")
    • Attempts to parse a Date from a string.

      Parameters

      • value: string

        The string to parse. Accepts various date formats understood by JavaScript Date constructor.

      Returns Result<Date>

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

      settings.tryStrToDate('2024-07-05');  // Ok(Date)
      settings.tryStrToDate('July 5, 2024'); // Ok(Date)
      settings.tryStrToDate('invalid'); // Err("Invalid date string")
    • Attempts to parse an integer from a string.

      Parameters

      • value: string

        The string to parse. Must contain only digits and an optional leading +/- sign.

      Returns Result<number>

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

      settings.tryStrToInt('123');    // Ok(123)
      settings.tryStrToInt('-456'); // Ok(-456)
      settings.tryStrToInt('12.34'); // Err("Invalid integer string")
    • Attempts to parse a number from a string using locale-specific formatting. Handles currency symbols, thousands separators, and locale-specific decimal separators.

      Parameters

      • value: string

        The string to parse.

      Returns Result<number>

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

      const usSettings = DotNetLocaleSettings.create('en-US');
      usSettings.tryStrToNumber('1,234.56'); // Ok(1234.56)
      usSettings.tryStrToNumber('$1,234.56'); // Ok(1234.56)

      const frSettings = DotNetLocaleSettings.create('fr-FR');
      frSettings.tryStrToNumber('1 234,56'); // Ok(1234.56)
    • Creates a DotNetLocaleSettings instance for the specified locale.

      Parameters

      • localeName: string

        The locale name (e.g., "en-US", "fr-FR", "de-DE").

      Returns DotNetLocaleSettings

      A new DotNetLocaleSettings instance configured for the specified locale.

      const usSettings = DotNetLocaleSettings.create('en-US');
      const frSettings = DotNetLocaleSettings.create('fr-FR');
    • Creates a DotNetLocaleSettings instance for the invariant culture (en-US). The invariant culture provides consistent formatting across all systems.

      Returns DotNetLocaleSettings

      A new DotNetLocaleSettings instance configured for invariant culture.

      const settings = DotNetLocaleSettings.createInvariant();
      console.log(settings.decimalSeparator); // "."
      console.log(settings.thousandSeparator); // ","