Skip to main content

array2string

Return a string representation of an array with full control over formatting.
function array2string(a: ArrayLike, options?: { max_line_width?: number; precision?: number; suppress_small?: boolean; separator?: string; prefix?: string; suffix?: string; threshold?: number; edgeitems?: number; sign?: ' ' | '+' | '-'; floatmode?: 'fixed' | 'unique' | 'maxprec' | 'maxprec_equal'; }): string
ParameterTypeDefaultDescription
aArrayLikeInput array.
options{ max_line_width?: number; precision?: number; suppress_small?: boolean; separator?: string; prefix?: string; suffix?: string; threshold?: number; edgeitems?: number; sign?: ' ' | '+' | '-'; floatmode?: 'fixed' | 'unique' | 'maxprec' | 'maxprec_equal'; }undefinedFormatting options.
Returns: string — Formatted string representation of the array.
import { array, array2string } from 'numpy-ts';

const a = array([[1.123456, 2.789], [3.14159, 4.0]]);

array2string(a);
// '[[1.123456, 2.789   ],\n [3.14159 , 4.      ]]'

array2string(a, { precision: 2, suppress: true });
// '[[1.12, 2.79],\n [3.14, 4.  ]]'

array_repr

Return the string representation of an array, including the array(...) wrapper, dtype, and shape information. This is what toString() uses internally.
function array_repr(
  a: ArrayLike,
  max_line_width?: number,
  precision?: number,
  suppress_small?: boolean
): string
ParameterTypeDefaultDescription
aArrayLikeInput array.
max_line_widthnumber75Maximum number of characters per line.
precisionnumber8Number of digits of precision for floating-point output.
suppress_smallbooleanfalseIf true, numbers very close to zero are printed as 0..
Returns: string — A string like array([[1, 2], [3, 4]]).
import { array, array_repr } from 'numpy-ts';

const a = array([[1, 2], [3, 4]]);

array_repr(a);
// 'array([[1, 2],\n       [3, 4]])'

array_repr(a, 40, 2);
// 'array([[1, 2],\n       [3, 4]])'

array_str

Return a string representation of the data in an array, without the array(...) wrapper.
function array_str(
  a: ArrayLike,
  max_line_width?: number,
  precision?: number
): string
ParameterTypeDefaultDescription
aArrayLikeInput array.
max_line_widthnumber75Maximum number of characters per line.
precisionnumber8Floating-point precision.
suppress_smallbooleanfalseIf true, suppress small floating-point values.
Returns: string — The array data as a string without the array() prefix.
import { array, array_str } from 'numpy-ts';

const a = array([1.5, 2.5, 3.5]);

array_str(a);
// '[1.5, 2.5, 3.5]'

base_repr

Return the string representation of a number in the given base.
function base_repr(number: number, base?: number, padding?: number): string
ParameterTypeDefaultDescription
numbernumberThe integer to convert.
basenumber2The base for the representation (2 through 36).
paddingnumber0Minimum number of digits. Pads with leading zeros if needed.
Returns: string — String representation in the specified base.
import { base_repr } from 'numpy-ts';

base_repr(10, 2);        // '1010'
base_repr(255, 16);      // 'FF'
base_repr(10, 8, 4);     // '0012'
base_repr(42, 10);       // '42'

binary_repr

Return the binary string representation of a number. Negative numbers use two’s complement notation.
function binary_repr(num: number, width?: null | number): string
ParameterTypeDefaultDescription
numnumberThe integer to convert.
widthnumberundefinedTotal width of the binary string. For negative numbers, this determines the two’s complement width.
Returns: string — Binary string representation.
import { binary_repr } from 'numpy-ts';

binary_repr(10);       // '1010'
binary_repr(3);        // '11'
binary_repr(-3, 8);    // '11111101'  (two's complement)
binary_repr(10, 8);    // '00001010'  (zero-padded)

format_float_positional

Format a floating-point scalar in positional (non-scientific) notation with fine-grained control.
function format_float_positional(
  x: number,
  precision?: null | number,
  unique?: boolean,
  fractional?: boolean,
  trim?: '-' | '.' | '0' | 'k',
  sign?: ' ' | '+' | '-',
  pad_left?: null | number,
  pad_right?: null | number,
  min_digits?: null | number
): string
ParameterTypeDefaultDescription
xnumberValue to format.
precisionnull | numberundefinedMaximum number of digits.
uniquebooleantrueIf true, use the minimum number of digits to uniquely represent the value.
fractionalbooleantrueIf true, precision refers to digits after the decimal point; otherwise total significant digits.
trim'-' | '.' | '0' | 'k''k'Controls trailing zero trimming.
sign' ' | '+' | '-''-'Sign formatting mode.
pad_leftnull | numberundefinedPad with spaces on the left to this width.
pad_rightnull | numberundefinedPad with spaces on the right to this width.
min_digitsnull | numberundefinedMinimum significant digits to emit.
Returns: string — Formatted number string.
import { format_float_positional } from 'numpy-ts';

format_float_positional(3.14);                   // '3.14'
format_float_positional(3.14, 4);                // '3.1400'
format_float_positional(3.14, 4, true, true, '-'); // '3.14'
format_float_positional(3.14, undefined, true, true, 'k', true); // '+3.14'

format_float_scientific

Format a floating-point scalar in scientific notation.
function format_float_scientific(
  x: number,
  precision?: null | number,
  _unique?: boolean,
  trim?: '-' | '.' | '0' | 'k',
  sign?: ' ' | '+' | '-',
  pad_left?: null | number,
  exp_digits?: number,
  min_digits?: null | number
): string
ParameterTypeDefaultDescription
xnumberValue to format.
precisionnull | numberundefinedNumber of digits of precision.
_uniquebooleantrueIf true, use minimal digits to uniquely represent the value.
trim'-' | '.' | '0' | 'k''k'Trailing zero trimming (same options as format_float_positional).
sign' ' | '+' | '-''-'Sign formatting mode.
pad_leftnull | numberundefinedPad with spaces on the left to this width.
exp_digitsnumberundefinedMinimum number of digits in the exponent.
min_digitsnull | numberundefinedMinimum significant digits to emit.
Returns: string — Formatted scientific notation string.
import { format_float_scientific } from 'numpy-ts';

format_float_scientific(1500.0);              // '1.5e+03'
format_float_scientific(0.00123, 2);          // '1.23e-03'
format_float_scientific(1500.0, 3, true, 'k', true, 3); // '+1.500e+003'

get_printoptions

Return the current default print options used by toString() and array2string.
function get_printoptions(): PrintOptions
Returns: PrintOptions — An object with the current settings including precision, threshold, edgeitems, linewidth, suppress, and nanstr.
import { get_printoptions } from 'numpy-ts';

const opts = get_printoptions();
console.log(opts.precision);   // 8
console.log(opts.threshold);   // 1000
console.log(opts.edgeitems);   // 3

set_printoptions

Set the default print options globally. These affect all subsequent calls to toString(), array_repr, and array2string.
function set_printoptions(options: Partial<PrintOptions>): void
ParameterTypeDefaultDescription
optionsPrintOptionsAn object with one or more print option keys: precision, threshold, edgeitems, linewidth, suppress, nanstr, infstr, sign, formatter.
Returns: void
import { array, set_printoptions } from 'numpy-ts';

set_printoptions({ precision: 2, suppress: true });

const a = array([0.000123, 1.23456, 789.0]);
console.log(a);
// array([0.  , 1.23, 789. ])

// Reset to defaults
set_printoptions({ precision: 8, suppress: false });

printoptions

Temporarily set print options and return a context object. This is useful as a scoped context manager for formatting.
function printoptions(options: Partial<PrintOptions>): { enter: () => void; exit: () => void; apply: <T>(fn: () => T) => T; _savedOptions: PrintOptions | null; }
ParameterTypeDefaultDescription
optionsPartial<PrintOptions>Temporary print options to apply.
Returns: { enter, exit, apply, _savedOptions } — Context helpers that apply and restore temporary print settings.
import { array, printoptions } from 'numpy-ts';

const a = array([1.23456789, 2.3456789]);

const ctx = printoptions({ precision: 2 });
ctx.enter();
console.log(a);
// array([1.23, 2.35])

ctx.exit();
console.log(a);
// array([1.23456789, 2.3456789 ])