File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ fn Round(n: f64): f64 {
2+ if n >= 0 {
3+ return f64(int(n + 0.5))
4+ }
5+ return f64(int(n - 0.5))
6+ }
Original file line number Diff line number Diff line change 1+ #build test
2+
3+ use "std/testing"
4+
5+ #test
6+ fn testRound(t: &testing::T) {
7+ t.Assert(Round(12.2) == 12, "12.2 should round to 12")
8+ t.Assert(Round(12.5) == 13, "12.5 should round to 13")
9+ t.Assert(Round(12.8) == 13, "12.8 should round to 13")
10+ t.Assert(Round(12.0) == 12, "12.0 should round to 12")
11+ t.Assert(Round(-12.2) == -12, "-12.2 should round to -12")
12+ t.Assert(Round(-12.5) == -13, "-12.5 should round to -13")
13+ t.Assert(Round(-12.8) == -13, "-12.8 should round to -13")
14+ t.Assert(Round(0.49) == 0, "0.49 should round to 0")
15+ t.Assert(Round(0.5) == 1, "0.5 should round to 1")
16+ t.Assert(Round(-0.49) == 0, "-0.49 should round to 0")
17+ t.Assert(Round(-0.5) == -1, "-0.5 should round to -1")
18+ }
You can’t perform that action at this time.
0 commit comments