Skip to content

Commit ed1978f

Browse files
committed
Round function was extended with optional parameter numdecimalplaces, increased compatibility with VBA implementation
1 parent 3abc905 commit ed1978f

4 files changed

Lines changed: 36 additions & 5 deletions

File tree

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

Lines changed: 28 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;
@@ -216,9 +220,30 @@ public static double rint(final double a) {
216220
return 0.0;
217221
}
218222

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;
223+
/**
224+
* Returns a number rounded to a specified number of decimal places.
225+
*
226+
* This function returns something commonly referred to as bankers rounding.
227+
* So be careful before using this function. Same behavior has 'round' in VBA.
228+
*
229+
* @param value Numeric value being rounded
230+
* @param numdecimalplaces Number indicating how many places to the right of the decimal
231+
* are included in the rounding.
232+
* @return rounded value
233+
*/
234+
@BasicFunction(classification = com.scriptbasic.classification.Math.class)
235+
static public Object round(final double value, Integer numdecimalplaces) {
236+
237+
BigDecimal bd = BigDecimal.valueOf(value);
238+
239+
if(numdecimalplaces==null||numdecimalplaces==0) {
240+
BigDecimal result = bd.setScale(0, RoundingMode.HALF_EVEN);
241+
return result.intValue();
242+
} else {
243+
MathContext mc = new MathContext(numdecimalplaces, RoundingMode.HALF_EVEN);
244+
BigDecimal result = bd.round(mc);
245+
return result.doubleValue();
246+
}
222247
}
223248

224249
@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)