round

def draw
  background(200)
  # map, mouseX between 0 and 5.
  ax = map(mouseX, 0, 100, 0, 5)
  ay = 66

  # Round the mapped number.
  bx = round(map(mouseX, 0, 100, 0, 5))
  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(format("%.2f", ax), ax, ay - 5)
  text(format("%.1f", bx), bx, by - 5)
end

p5.jsリファレンス(参考情報)

[p5.js] round

概要

四捨五入した値を計算します。

書式

r = round(n [, decimals])

引数

引数名内容備考オプションデフォルト値
n数値
decimals小数点以下桁数0

戻値

r : 四捨五入された値

備考

関連