How to use @phensley/decimal - 10 common examples

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-compiler / src / cli / generator / code / plurals.ts View on Github external
const generate = (sample: string): string[] => {
  if (sample.indexOf('~') === -1) {
    return [sample];
  }
  const res: string[] = [];
  const [_s, _e] = sample.split('~');
  let s = new Decimal(_s);
  const e = new Decimal(_e);
  let incr = DecimalConstants.ONE;
  if (s.scale() > 0) {
    incr = incr.movePoint(-s.scale());
  }
  while (s.compare(e) !== 0) {
    res.push(s.toString());
    s = s.add(incr);
  }
  // console.log(`${s.toString()} ${e.toString()} incr ${incr.toString()}`;
  return res;
};
github phensley / cldr-engine / packages / cldr-compiler / src / cli / generator / code / plurals.ts View on Github external
const generate = (sample: string): string[] => {
  if (sample.indexOf('~') === -1) {
    return [sample];
  }
  const res: string[] = [];
  const [_s, _e] = sample.split('~');
  let s = new Decimal(_s);
  const e = new Decimal(_e);
  let incr = DecimalConstants.ONE;
  if (s.scale() > 0) {
    incr = incr.movePoint(-s.scale());
  }
  while (s.compare(e) !== 0) {
    res.push(s.toString());
    s = s.add(incr);
  }
  // console.log(`${s.toString()} ${e.toString()} incr ${incr.toString()}`;
  return res;
};
github phensley / cldr-engine / packages / cldr-compiler / src / cli / generator / code / plurals.ts View on Github external
const generate = (sample: string): string[] => {
  if (sample.indexOf('~') === -1) {
    return [sample];
  }
  const res: string[] = [];
  const [_s, _e] = sample.split('~');
  let s = new Decimal(_s);
  const e = new Decimal(_e);
  let incr = DecimalConstants.ONE;
  if (s.scale() > 0) {
    incr = incr.movePoint(-s.scale());
  }
  while (s.compare(e) !== 0) {
    res.push(s.toString());
    s = s.add(incr);
  }
  // console.log(`${s.toString()} ${e.toString()} incr ${incr.toString()}`;
  return res;
};
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-ext-rbnf / src / rbnf.ts View on Github external
RuleType,
  RBNFInst,
  RBNFRule,
  SubLeftInst,
} from './rbnftypes';
import { binarySearch } from './utils';

// Divisors based on the number of integer digits in the number being formatted.
// A 2-digit number's divisor will be '1e1', 3-digit '1e2' and so on.
// This table stops just past the largest base value found in the RBNF dataset.
const DIVISORS: Decimal[] = [0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
  .map(e => new Decimal(`1e${e}`));

const { ONE, ZERO } = DecimalConstants;
const MINUS_ONE = ONE.negate();
const TEN = new Decimal(10);

type IntegerSubInst = ApplyLeftRuleInst
  | ApplyLeft2RuleInst
  | ApplyLeft2NumFormatInst
  | ApplyLeftNumFormatInst
  | SubLeftInst;

export interface RBNFSymbols {
  decimal: string;
  nan: string;
  infinity: string;
}

export type RBNFDecimalFormatter =
  (pattern: string, n: Decimal) => string;
github phensley / cldr-engine / packages / messageformat / src / evaluation / evaluation.ts View on Github external
} from '../parser';

export type MessageFormatFunc =
  (args: MessageArg[], options: string[]) => string;

export type MessageFormatFuncMap = { [name: string]: MessageFormatFunc };

const get = (key: number | string, args: MessageArgs): MessageArg => {
  const res: MessageArg = args.named[key];
  return res !== undefined ? res : (typeof key === 'number' ? args.positional[key] : undefined);
};

// Save a bit of processing of common exact matches
const DECIMAL_EXACT: { [n: string]: Decimal } = {
  0: DecimalConstants.ZERO,
  1: DecimalConstants.ONE,
  2: DecimalConstants.TWO
};

/**
 * Evaluates a message format against a set of arguments, producing a string.
 */
export class MessageEngine {

  private buf: string = '';

  constructor(
    private plurals: PluralRules,
    private formatters: MessageFormatFuncMap,
    private code: MessageCode) { }

  evaluate(positional: MessageArg[], named: MessageNamedArgs = {}): string {
github phensley / cldr-engine / packages / cldr-core / src / internals / datefields / internal.ts View on Github external
}

    } else if (!options.numericOnly) {
      switch (field) {
        case 'hour':
        case 'minute':
        case 'second':
          break;
        default:
          if (n.compare(DecimalConstants.TWO) === 0) {
            const p = negative ? format.previous2.get(bundle, field) : format.next2.get(bundle, field);
            if (p !== '') {
              res = p;
            }
            // Fall through
          } else if (n.compare(DecimalConstants.ONE) === 0) {
            res = negative ? format.previous.get(bundle, field) : format.next.get(bundle, field);
          }
          break;
      }
    }

    // If we output anything above, return it
    if (res) {
      if (options.context) {
        res = this.internals.general.contextTransform(res, transform,
          options.context, 'relative');
        }
        return res;
    }

    // Format a pluralized future / past.