Puppet Function: round

Defined in:
lib/puppet/parser/functions/round.rb
Function type:
Ruby 3.x API

Summary

Rounds a number to the nearest integer

Overview

round()Any

“‘round(2.9)“` returns “`3“`

“‘round(2.4)“` returns “`2“`

> Note: from Puppet 6.0.0, the compatible function with the same name in Puppet core

will be used instead of this function.

Returns:

  • (Any)

    the rounded value as integer



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/puppet/parser/functions/round.rb', line 7

newfunction(:round, type: :rvalue, doc: <<-DOC
  @summary
    Rounds a number to the nearest integer

  @return
    the rounded value as integer

  @example

  ```round(2.9)``` returns ```3```

  ```round(2.4)``` returns ```2```

  > *Note:* from Puppet 6.0.0, the compatible function with the same name in Puppet core
    will be used instead of this function.
DOC
) do |args|
  raise Puppet::ParseError, "round(): Wrong number of arguments given #{args.size} for 1" if args.size != 1
  raise Puppet::ParseError, "round(): Expected a Numeric, got #{args[0].class}" unless args[0].is_a? Numeric

  value = args[0]

  if value >= 0
    Integer(value + 0.5)
  else
    Integer(value - 0.5)
  end
end