Macaulay2 » Documentation
Packages » Macaulay2Doc » The Macaulay2 language » strings and nets » format
next | previous | forward | backward | up | index | toc

# format -- format a string or real number

## Description

## Synopsis

- Usage:
    `format s`
- Inputs:
  - s, a string,
- Outputs:
  - a string, obtained from `s` by inserting escape sequences, thereby preparing it for printing in a format suitable for reading in again

    i1 : s = "a\"b\"c"

    o1 = a"b"c

    i2 : t = format s

    o2 = "a\"b\"c"

    i3 : u = value t

    o3 = a"b"c

    i4 : u == s

    o4 = true

The characters that are escaped are double quotes, backslashes, and all control characters. In particular, the outputted string will be valid JSON.

    i5 : format(ascii(0..31) | "\"\\")

    o5 = "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\
         u000f\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\
         u001b\u001c\u001d\u001e\u001f\"\\"

## Synopsis

- Usage:
    `format(s,k,l,t,e,x)`
- Inputs:
  - s, an integer, the maximum number of significant decimal digits (default: printingPrecision). The special value `0` imposes no limit.
  - k, an integer, how far to the right of the decimal point to go, at most (default: printingAccuracy). The special value `-1` imposes no limit.
  - l, an integer, maximum number of leading zeroes (default: printingLeadLimit).
  - t, an integer, maximum number of trailing zeroes (default: printingTrailLimit)
  - e, a string, the separator between the mantissa and the exponent (default: printingSeparator)
  - x, a real number, the number to be converted to a string
- Outputs:
  - a string, the decimal representation of the number `x`, prepared according to the parameters above

    i6 : format(10,1/3000.)

    o6 = .0003333333333

    i7 : format(10,6,1/3000.)

    o7 = .000333

    i8 : format(10,6,2,1/3000.)

    o8 = 3.33e-4

    i9 : format(10,300000.)

    o9 = 300000

    i10 : format(10,-1,10,5,300000.)

    o10 = 300000

    i11 : format(10,-1,10,4,300000.)

    o11 = 3e5

    i12 : format(10,-1,10,4,"E",300000.)

    o12 = 3E5

## See also

- toExternalString -- convert to a readable string

## Ways to use format:

- `format(DocumentTag)` -- see DocumentTag -- the class of all document tags
- `format(CC)`
- `format(RR)`
- `format(String)`

## For the programmer

The object format is a method function with a single argument.

The source of this document is in /build/macaulay2-88fgJW/macaulay2-1.25.11+ds/M2/Macaulay2/packages/Macaulay2Doc/functions/format-doc.m2:71:0.