GLSL built-in function

round()

Rounds to the nearest integer with implementation-chosen half-way behavior.

Demo version: GLSL ES 3.00 / WebGL 2Demo stage: Fragment

Syntax

genType round(genType x)

genType means float / vec2 / vec3 / vec4; matching placeholders use the same type.

Parameters

x
Input value or left-hand vector

Return value

The nearest integer-valued float for each component; the direction for exact half-way cases is implementation-dependent.

Live example

Edit the code and the preview updates as you type.

What round does

Rounds to the nearest integer with implementation-chosen half-way behavior.

Read the preview

round(x*4.0)/4.0 draws levels separated by 0.25. Change both 4.0 values to 2.0 to make the steps twice as tall and wide.

Core call

round(x*4.0)/4.0;

Do not depend on one tie direction at n+0.5; use roundEven for ties to even.

Experiment

Use round(0.5)/4.0 as the result expression. The preview becomes a constant line; its level may depend on the implementation's permitted choice for the half-way case.

Specification

GLSL ES 3.00 built-in functions, chapter 8

Related functions