Update stdlib
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / round.rb
1 #
2 # round.rb
3 #
4
5 module Puppet::Parser::Functions
6   newfunction(:round, :type => :rvalue, :doc => <<-EOS
7   Rounds a number to the nearest integer
8
9   *Examples:*
10
11   round(2.9)
12
13   returns: 3
14
15   round(2.4)
16
17   returns: 2
18
19   EOS
20   ) do |args|
21
22     raise Puppet::ParseError, "round(): Wrong number of arguments given #{args.size} for 1" if args.size != 1
23     raise Puppet::ParseError, "round(): Expected a Numeric, got #{args[0].class}" unless args[0].is_a? Numeric
24
25     value = args[0]
26
27     if value >= 0
28       Integer(value + 0.5)
29     else
30       Integer(value - 0.5)
31     end
32   end
33 end