From eb9f06f9c15e29342b0bbb460058814b44826ccb Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Sun, 20 May 2012 11:17:44 +0700 Subject: [PATCH] MDL-29173 grade: altered the definiton of round() in /lib/evalmath/evalmath.class.php --- lib/evalmath/evalmath.class.php | 2 +- lib/tests/mathslib_test.php | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/evalmath/evalmath.class.php b/lib/evalmath/evalmath.class.php index aa5539fe186..9d9c7d6250b 100644 --- a/lib/evalmath/evalmath.class.php +++ b/lib/evalmath/evalmath.class.php @@ -107,7 +107,7 @@ class EvalMath { 'sin','sinh','arcsin','asin','arcsinh','asinh', 'cos','cosh','arccos','acos','arccosh','acosh', 'tan','tanh','arctan','atan','arctanh','atanh', - 'sqrt','abs','ln','log','exp','floor','ceil','round'); + 'sqrt','abs','ln','log','exp','floor','ceil'); var $fc = array( // calc functions emulation 'average'=>array(-1), 'max'=>array(-1), 'min'=>array(-1), diff --git a/lib/tests/mathslib_test.php b/lib/tests/mathslib_test.php index 4c56eed2196..f541e61b127 100644 --- a/lib/tests/mathslib_test.php +++ b/lib/tests/mathslib_test.php @@ -142,6 +142,10 @@ class mathsslib_testcase extends basic_testcase { } public function test_rounding_function() { + + // Rounding to the default number of decimal places + // The default == 0 + $formula = new calc_formula('=round(2.5)'); $this->assertEquals($formula->evaluate(), 3); @@ -196,6 +200,19 @@ class mathsslib_testcase extends basic_testcase { $formula = new calc_formula('=floor(-2.5)'); $this->assertEquals($formula->evaluate(), -3); + // Rounding to an explicit number of decimal places + + $formula = new calc_formula('=round(2.5, 1)'); + $this->assertEquals($formula->evaluate(), 2.5); + + $formula = new calc_formula('=round(2.5, 0)'); + $this->assertEquals($formula->evaluate(), 3); + + $formula = new calc_formula('=round(1.2345, 2)'); + $this->assertEquals($formula->evaluate(), 1.23); + + $formula = new calc_formula('=round(123.456, -1)'); + $this->assertEquals($formula->evaluate(), 120); } public function test_scientific_notation() {