Static p5_sys::global::round[][src]

pub static round: RoundInternalType
Expand description

Calculates the integer closest to the n parameter. For example, round(133.8) returns the value 134. Maps to Math.round().

Examples

let x = round(3.7);
text(x, width / 2, height / 2);
let x = round(12.782383, 2);
text(x, width / 2, height / 2);
function draw() {
  background(200);
  //map, mouseX between 0 and 5.
  let ax = map(mouseX, 0, 100, 0, 5);
  let ay = 66;

  // Round the mapped number.
  let bx = round(map(mouseX, 0, 100, 0, 5));
  let by = 33;

  // Multiply the mapped numbers by 20 to more easily
  // see the changes.
  stroke(0);
  fill(0);
  line(0, ay, ax * 20, ay);
  line(0, by, bx * 20, by);

  // Reformat the float returned by map and draw it.
  noStroke();
  text(nfc(ax, 2), ax, ay - 5);
  text(nfc(bx, 1), bx, by - 5);
}

Parameters

n number to round

decimals? number of decimal places to round to, default is 0