G-codeUnits & ModesModal group 6Mill · Lathe · 3D printer

G20 & G21: Inch and Metric Units

G21 has the control read every coordinate, feed and offset in the program as millimetres, G20 as inches. The two share a modal group, so one of them is always live, and the one a machine wakes up in is set by a parameter rather than by anything a programmer wrote. That is where the danger sits: a program that never states its units runs in whatever the last job left behind, and a 25mm move becomes a 635mm move with nothing raising an alarm. Write the unit word first, in a block of its own, above the first coordinate.

Syntax

G21             Input in millimetres, feed in mm/min or mm/rev
G20             Input in inches, feed in inch/min or inch/rev

Both are modal and both belong to the same group, so writing one cancels the other. Neither takes an address.

Parameters

Neither code carries a value. What the mode decides is the unit every other number on the page is read in, and two of the rows below hold their old units through the change.

ValueUnder G21Under G20
X Y Z coordinateMillimetresInches, so X25. is 635mm
F under G94Millimetres per minuteInches per minute
F under G95Millimetres per revolutionInches per revolution
R and Z in a canned cycleMillimetresInches
Q peck depthMillimetresInches
S under G97Revolutions per minuteRevolutions per minute, unchanged by the mode
S under G96Metres per minuteSurface feet per minute
Tool length and work offset registersA stored 125.6 is 125.6mmA stored 125.6 is 125.6 inches. The table is never converted
Least input increment, Fanuc IS-B0.001mm0.0001in

Example

O0011 (CLAMP PLATE, 20MM 6082, SPOT AND DRILL 4 HOLES FOR M10)
G21                              (millimetres. Under G20 every number below is 25.4 times bigger)
G17 G40 G49 G80 G90              (rest of the safe start block)
T02 M06                          (10mm 90 degree spot drill)
G54
S3200 M03                        (100m/min. S is rpm in either unit mode)
G00 X20. Y20.                    (first hole)
G43 H02 Z25. M08                 (tool length comp on, flood coolant)
G99 G81 Z-2.6 R2. F320           (2.6mm spot, 5.2mm across, 0.1mm/rev)
X90.                             (second)
Y70.                             (third)
X20.                             (fourth)
G80
G00 Z25. M09
M05
T03 M06                          (8.5mm carbide drill, tapping size for M10 x 1.5)
S2400 M03                        (64m/min)
G00 X20. Y20.
G43 H03 Z25. M08
G99 G83 Z-23. R2. Q5. F360       (3mm through the back face, 5mm pecks, 0.15mm/rev)
X90.
Y70.
X20.
G80
G00 Z25. M09
G49
G91 G28 Z0.                      (home Z)
M30

Controller differences

ControlBehaviour
FanucThe pair sits in modal group 06, and the manual's instruction is to write one of them in a block of its own at the start of the program, before the coordinate system is set, and to leave it alone after that. A switch does not convert what is already stored: a tool length held as 125.6 stays the number 125.6 and is simply read in the new unit. The state at power-on is a machine parameter, so the same file behaves differently on two machines.
HaasThe unit system belongs to Setting 9, Dimensioning, not to the program. G20 and G21 are there to assert what the program expects and the control checks them against that setting instead of switching over, so a metric program on a machine set to INCH stops rather than scaling itself. Changing Setting 9 by hand leaves the numbers in the offset tables exactly as they were.
Siemens 840DThe native language has no G20 or G21. Inch is G70, metric G71, and those two convert the geometry only. G700 and G710 switch the feedrate, the tool offsets and the rest of the technology values along with it, which is usually what was wanted. G70 and G71 are the finishing and rough turning cycles on a Fanuc lathe, so a post aimed at the wrong dialect writes blocks that run and cut something nobody drew. G20 and G21 work in ISO dialect mode.
MazakEIA/ISO programs take both codes as on Fanuc. A Mazatrol conversational program has no unit word at all: the unit system is machine data and the dimensions in each unit follow whatever the control is set to display.
GrblBoth are supported and metric is the state the parser comes up in. What the machine reports is separate: $13 switches the status report to inches and leaves the parser in G21, so a DRO counting inches sits above a machine moving in millimetres. The $ settings, maximum rate and acceleration among them, are held in mm and G20 does not touch them.
MarlinG20 and G21 exist only where INCH_MODE_SUPPORT is enabled in Configuration_adv.h. Stock firmware answers an unknown command and carries straight on in millimetres, so an inch-dimensioned job runs at a twenty-fifth of its size and finishes without complaint.

What goes wrong

A rapid that drives the table into its hard stop is usually a program that never said which units it wanted. The mode survives between jobs, so a metric file loaded behind an inch job reads X25. as 25 inches, 635mm, and Z-5. as 127mm down. The feed goes with it: F250 becomes 250 inch/min, 6350mm/min. Nothing alarms on any of it. The unit word belongs on the first line, by itself, above every coordinate.

Second is the offset tables, which hold their numbers through the change. Flip the mode, or Setting 9 on a Haas, and a tool length of 125.6 is no longer 125.6mm but 125.6 inches, 3190mm of offset on a machine with 500mm of Z travel. Work offsets do the same. Every number you lean on has to be read back off the page or typed in again.

Third is switching part way down the program. Fanuc puts the code at the top in a block of its own and forbids a change after that, because the coordinate system, the offsets and the stored axis positions are all still carrying values in the system they were entered in.

Fourth, the readout and the program disagree. Grbl's $13 sets the report to inches while the parser stays in G21, so an operator trimming a feed by what the screen says is out by a factor of 25.4.

Last, an integer with no decimal point. In inch input on a Fanuc the least increment is a ten thousandth, so a bare X5 is 0.0127mm (0.0005in), and the tool never reaches the material.

Updated 30 Sep 2026