Skip to content

Commit 39ea2c0

Browse files
committed
Add a round function to complement ceiling and floor
1 parent 596878a commit 39ea2c0

3 files changed

Lines changed: 24 additions & 0 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,6 +1509,10 @@ For example, `reject(['aaa','bbb','ccc','aaaddd'], 'aaa')` returns ['bbb','ccc']
15091509
15101510
Reverses the order of a string or array.
15111511
1512+
#### `stdlib::round`
1513+
1514+
Rounds a number to the nearest integer
1515+
15121516
*Type*: rvalue.
15131517
15141518
#### `rstrip`

functions/round.pp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function stdlib::round(
2+
Numeric $input,
3+
) {
4+
if $input >= 0 {
5+
Integer( $input + 0.5 )
6+
} else {
7+
Integer( $input - 0.5 )
8+
}
9+
}

spec/functions/round_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'spec_helper'
2+
3+
describe 'stdlib::round' do
4+
it { is_expected.not_to eq(nil) }
5+
it { is_expected.to run.with_params(34.3).and_return(34) }
6+
it { is_expected.to run.with_params(-34.3).and_return(-34) }
7+
it { is_expected.to run.with_params(34.5).and_return(35) }
8+
it { is_expected.to run.with_params(-34.5).and_return(-35) }
9+
it { is_expected.to run.with_params(34.7).and_return(35) }
10+
it { is_expected.to run.with_params(-34.7).and_return(-35) }
11+
end

0 commit comments

Comments
 (0)