How to use the @phensley/decimal.coerceDecimal function in @phensley/decimal

To help you get started, we’ve selected a few @phensley/decimal examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github phensley / cldr-engine / packages / cldr-core / src / api / numbers.ts View on Github external
protected formatCurrencyImpl(renderer: NumberRenderer, params: NumberParams,
      n: DecimalArg, code: CurrencyType, options: CurrencyFormatOptions): T {

    // Not much to be done with NaN and Infinity with currencies, so we always
    // throw an error.
    const d = coerceDecimal(n);
    validate(d, FORCE_ERRORS, renderer, params);
    return this.numbers.formatCurrency(this.bundle, renderer, coerceDecimal(n), code, options, params);
  }
github phensley / cldr-engine / packages / cldr-core / src / internals / units / internal.ts View on Github external
format(bundle: Bundle, renderer: NumberRenderer, q: Quantity,
    options: UnitFormatOptions, params: NumberParams): T {

    const n = coerceDecimal(q.value);
    const [num, plural] = this.internals.numbers.formatDecimal(bundle, renderer, n, options, params);
    if (q.unit === undefined) {
      return num;
    }

    // Compute plural category for the value '1'
    const singular = bundle.plurals().cardinal(DecimalConstants.ONE);

    // For default and "per" compound pattern, the {0} will use
    // the plural category and {1} will be singular. Examples:
    //   1 meter per second
    //  10 meters per second
    //
    // For the 'times' compound pattern, the {0} will be singular,
    // and the {1} will use the plural category. Examples:
    //   1 newton-meter
github phensley / cldr-engine / packages / cldr-core / src / api / numbers.ts View on Github external
protected formatDecimalImpl(renderer: NumberRenderer, params: NumberParams,
      n: DecimalArg, options: DecimalFormatOptions): T {

    // A NaN or Infinity value will just return the locale's representation
    const d = coerceDecimal(n);
    const v = validate(d, options, renderer, params);
    if (v !== undefined) {
      return v;
    }
    const [result] = this.numbers.formatDecimal(this.bundle, renderer, d, options, params);
    return result;
  }
github phensley / cldr-engine / packages / cldr-core / src / api / numbers.ts View on Github external
protected formatCurrencyImpl(renderer: NumberRenderer, params: NumberParams,
      n: DecimalArg, code: CurrencyType, options: CurrencyFormatOptions): T {

    // Not much to be done with NaN and Infinity with currencies, so we always
    // throw an error.
    const d = coerceDecimal(n);
    validate(d, FORCE_ERRORS, renderer, params);
    return this.numbers.formatCurrency(this.bundle, renderer, coerceDecimal(n), code, options, params);
  }
github phensley / cldr-engine / packages / cldr-core / src / api / numbers.ts View on Github external
adjustDecimal(n: DecimalArg, opts?: DecimalAdjustOptions): Decimal {
    return this.numbers.adjustDecimal(coerceDecimal(n), opts);
  }
github phensley / cldr-engine / packages / cldr-core / src / internals / datefields / internal.ts View on Github external
formatRelativeTimeField(bundle: Bundle, value: DecimalArg, field: RelativeTimeFieldType,
      options: RelativeTimeFormatOptions, params: NumberParams,
      transform: ContextTransformInfo): string {

    const width = options.width || 'wide';
    const format: RelativeTimeFields = this.relativeTimes[width] || this.relativeTimes.wide;
    const group = options.group === undefined ? true : options.group;

    let n = coerceDecimal(value);
    n = this.internals.numbers.adjustDecimal(n, options);
    const negative = n.isNegative();
    if (negative) {
      n = n.negate();
    }

    const iszero = n.compare(DecimalConstants.ZERO) === 0;
    let res = '';

    if (iszero) {
      if (options.alwaysNow || !options.numericOnly) {
        res = format.current.get(bundle, field);
      }

    } else if (!options.numericOnly) {
      switch (field) {
github phensley / cldr-engine / packages / cldr-core / src / api / numbers.ts View on Github external
getPluralOrdinal(n: DecimalArg, options?: DecimalAdjustOptions): string {
    const d = options ? this.adjustDecimal(n, options) : coerceDecimal(n);
    return this.bundle.plurals().ordinal(d);
  }
github phensley / cldr-engine / packages / cldr-core / src / internals / calendars / patterns.ts View on Github external
getAvailablePattern(d: CalendarDate, s: DateSkeleton): DateTimeNode[] {
    let plural: PluralType = 'other';
    let pattern = s.pattern;
    if (!pattern) {
      switch (s.skeleton) {
        case 'MMMMW': {
          const week = coerceDecimal(d.weekOfMonth());
          plural = this.bundle.plurals().cardinal(week) as PluralType;
          pattern = this.rawPluralFormats[plural][s.skeleton];
          break;
        }
        case 'yw': {
          const week = coerceDecimal(d.weekOfYear());
          plural = this.bundle.plurals().cardinal(week) as PluralType;
          pattern = this.rawPluralFormats[plural][s.skeleton];
          break;
        }
        default:
          pattern = this.rawAvailableFormats[s.skeleton];
          break;
      }
    }
    return this.internals.calendars.parseDatePattern(pattern || '');
  }