1. cvi

Converts a number or string to an integer.

1.1. Syntax

num cvi → int
string cvi → int

1.2. Stack Effects

Table 1. Before
Level Object

0

num (integer or real) or string

Table 2. After
Level Object

0

int (integer result)

1.3. Description

cvi (convert to integer) converts an integer, real, or string to an integer result:

  • Integer operand: Returns it unchanged

  • Real operand: Truncates fractional part (rounds toward 0) and converts to integer

  • String operand: Parses as PostScript number syntax, then converts to integer if needed

A [rangecheck] error occurs if a real is too large to convert to an integer.

1.4. PostScript Level

Level 1 and later

1.5. Examples

Converting from string
(3.3E1) cvi      % Result: 33
(123) cvi        % Result: 123
(-456) cvi       % Result: -456
Converting from real
-47.8 cvi        % Result: -47
520.9 cvi        % Result: 520
3.14159 cvi      % Result: 3
Integer pass-through
42 cvi           % Result: 42 (unchanged)

1.6. Common Use Cases

1.6.1. String to Integer Parsing

(100) cvi /width exch def
(200) cvi /height exch def

1.6.2. Truncating Reals

x cvi  % Remove fractional part

1.6.3. Array Index Calculation

position cellsize div cvi  % Integer cell index

1.7. Common Pitfalls

Truncation, Not Rounding - cvi truncates toward zero, not to nearest integer.
3.9 cvi          % Result: 3 (not 4)
-3.9 cvi         % Result: -3 (not -4)
Range Limits - Very large reals cannot be converted.
1.0e100 cvi      % Error: rangecheck
String Syntax - String must contain valid PostScript number syntax.
(abc) cvi        % Error: syntaxerror
(12x) cvi        % Error: syntaxerror
Use round/floor/ceiling - For other rounding behavior, use round, floor, or ceiling before cvi.

1.8. Error Conditions

Error Condition

[invalidaccess]

String has no-access attribute

[rangecheck]

Real too large for integer conversion

[stackunderflow]

No operand on stack

[syntaxerror]

String not valid number syntax

[typecheck]

Operand not number or string

[undefinedresult]

String parses to invalid number

1.9. Implementation Notes

  • Truncation performed before conversion

  • Integer range typically ±2³¹-1 (32-bit implementations)

  • String parsing follows full PostScript syntax (including radix notation)

  • Same conversion as implicit coercion in many operators

1.10. Comparison with Other Conversion Operators

Operator Result

cvi

Truncate toward 0, convert to integer

cvr

Convert to real

round

Round to nearest, keep type

floor

Round down, keep type

ceiling

Round up, keep type

truncate

Round toward 0, keep type

1.11. See Also


Back to top

Copyright © 2025 Ribose. PostScript is a trademark of Adobe. Distributed under the MIT License.