yq/pkg/yqlib/doc/operators/modulo.md
William Floyd 30e16a33c3
Fix for #2677 (#2705)
* Update docs given https://github.com/yaml/go-yaml/pull/348

* Fix for https://github.com/mikefarah/yq/issues/2677

Depends on https://github.com/yaml/go-yaml/pull/348

* Test for https://github.com/mikefarah/yq/issues/2677

* Remove redundant check and add test case for explicit `!!merge` on `*+` traversal

* Bump go.yaml.in/yaml/v4 from 4.0.0-rc.4 to 4.0.0-rc.5
2026-06-09 14:08:47 +10:00

1.0 KiB

Modulo

Arithmetic modulo operator, returns the remainder from dividing two numbers.

Number modulo - int

If the lhs and rhs are ints then the expression will be calculated with ints.

Given a sample.yml file of:

a: 13
b: 2

then

yq '.a = .a % .b' sample.yml

will output

a: 1
b: 2

Number modulo - float

If the lhs or rhs are floats then the expression will be calculated with floats.

Given a sample.yml file of:

a: 12
b: 2.5

then

yq '.a = .a % .b' sample.yml

will output

a: 2
b: 2.5

Number modulo - int by zero

If the lhs is an int and rhs is a 0 the result is an error.

Given a sample.yml file of:

a: 1
b: 0

then

yq '.a = .a % .b' sample.yml

will output

Error: cannot modulo by 0

Number modulo - float by zero

If the lhs is a float and rhs is a 0 the result is NaN.

Given a sample.yml file of:

a: 1.1
b: 0

then

yq '.a = .a % .b' sample.yml

will output

a: NaN
b: 0