yq
v4.x
v4.x
  • yq
  • How It Works
  • Recipes
  • Upgrading from V3
  • Commands
    • Evaluate
    • Evaluate All
    • Shell Completion
  • Operators
    • Add
    • Alternative (Default value)
    • Anchor and Alias Operators
    • Array to Map
    • Assign (Update)
    • Boolean Operators
    • Collect into Array
    • Column
    • Comment Operators
    • Compare Operators
    • Contains
    • Create, Collect into Object
    • Date Time
    • Delete
    • Divide
    • Document Index
    • Encode / Decode
    • Entries
    • Env Variable Operators
    • Equals
    • Eval
    • File Operators
    • Filter Operator
    • Flatten
    • Group By
    • Has
    • Keys
    • Kind
    • Length
    • Line
    • Load
    • Min
    • Map
    • Max
    • Modulo
    • Multiply (Merge)
    • Omit
    • Parent
    • Path
    • Pick
    • Pipe
    • Pivot
    • Recursive Descent (Glob)
    • Reduce
    • Reverse
    • Select
    • Shuffle
    • Slice Array
    • Sort
    • Sort Keys
    • Split into Documents
    • String Operators
    • Style
    • Subtract
    • Tag
    • To Number
    • Traverse (Read)
    • Union
    • Unique
    • Variable Operators
    • With
  • Usage
    • Output format
    • Working with CSV, TSV
    • Working with JSON
    • Working with Properties
    • Working with XML
    • Working with LUA
    • Working with TOML
    • Working with Shell Output
    • Front Matter
    • Split into multiple files
    • GitHub Action
    • Tips, Tricks, Troubleshooting
  • Github Page
Powered by GitBook
On this page
  • Number modulo - int
  • Number modulo - float
  • Number modulo - int by zero
  • Number modulo - float by zero

Was this helpful?

  1. Operators

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: !!float 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: !!float NaN
b: 0
PreviousMaxNextMultiply (Merge)

Last updated 2 years ago

Was this helpful?