Skip to content

Commit 7a19b39

Browse files
authored
Merge pull request #21 from PetrPytelka/improved_round_function
Round function was extended with optional parameter numdecimalplaces
2 parents 34db8bd + c5e099d commit 7a19b39

4 files changed

Lines changed: 39 additions & 5 deletions

File tree

src/main/java/com/scriptbasic/utility/functions/MathFunctions.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.scriptbasic.utility.functions;
22

3+
import java.math.BigDecimal;
4+
import java.math.MathContext;
5+
import java.math.RoundingMode;
6+
37
import com.scriptbasic.api.BasicFunction;
48
import com.scriptbasic.api.ScriptBasicException;
59
import com.scriptbasic.interfaces.BasicRuntimeException;
@@ -21,6 +25,11 @@
2125
@SuppressWarnings({"SameReturnValue", "UnusedReturnValue"})
2226
public class MathFunctions {
2327

28+
/**
29+
* Rounding mode used by round function
30+
*/
31+
static public RoundingMode roundingMode = RoundingMode.HALF_EVEN;
32+
2433
private MathFunctions() {
2534
NoInstance.isPossible();
2635
}
@@ -216,9 +225,28 @@ public static double rint(final double a) {
216225
return 0.0;
217226
}
218227

219-
@BasicFunction(substituteClass = java.lang.Math.class, classification = com.scriptbasic.classification.Math.class)
220-
static public double round(final double x) {
221-
return 0.0;
228+
/**
229+
* Returns a number rounded to a specified number of decimal places.
230+
*
231+
* This function returns something commonly referred to as bankers rounding.
232+
* So be careful before using this function. Same behavior has 'round' in VBA.
233+
*
234+
* @param value Numeric value being rounded
235+
* @param numdecimalplaces Number indicating how many places to the right of the decimal
236+
* are included in the rounding.
237+
* @return rounded value
238+
*/
239+
@BasicFunction(classification = com.scriptbasic.classification.Math.class)
240+
static public Number round(final double value, Integer numdecimalplaces) {
241+
242+
final BigDecimal bd = BigDecimal.valueOf(value);
243+
244+
if (numdecimalplaces == null || numdecimalplaces == 0) {
245+
return bd.setScale(0, roundingMode).intValue();
246+
} else {
247+
final MathContext mc = new MathContext(numdecimalplaces, roundingMode);
248+
return bd.round(mc).doubleValue();
249+
}
222250
}
223251

224252
@BasicFunction(substituteClass = java.lang.Math.class, classification = com.scriptbasic.classification.Math.class)

src/test/java/com/scriptbasic/test/auxilliary/TestMathFunctions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void testExMethods() throws
8787

8888
pow(x, y);
8989

90-
round(x);
90+
round(x, 0);
9191

9292
tan(x);
9393

src/test/resources/com/scriptbasic/testprograms/TestMath.bas

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ assert("cos(0)=1", cos(0.0)=1.0)
66
assert("sin(0)=0", sin(0.0)=0.0)
77
assert("acos(1.0)=0", acos(1.0)=0.0)
88
assert("asin(0)=0",asin(0.0)=0.0)
9-
assert("strrev",strreverse("abc")="cba")
9+
assert("round0_5",round(0.5)=0)
10+
assert("round1_5",round(1.5)=2)
11+
assert("round0_14",round(0.14,1)=0.1)
12+
assert("round0_15",round(0.15,1)=0.2)
13+
assert("round0_16",round(0.16,1)=0.2)

src/test/resources/com/scriptbasic/testprograms/TestStringFunctions.bas

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ v = "0123456789"
22
PRINT left(v,2)
33
PRINT RIGHT(v,2)
44
PRINT Mid(v,2,3)
5+
6+
assert("strrev",strreverse("abc")="cba")

0 commit comments

Comments
 (0)