|
25 | 25 | @SuppressWarnings({"SameReturnValue", "UnusedReturnValue"}) |
26 | 26 | public class MathFunctions { |
27 | 27 |
|
| 28 | + /** |
| 29 | + * Rounding mode used by round function |
| 30 | + */ |
| 31 | + static public RoundingMode roundingMode = RoundingMode.HALF_EVEN; |
| 32 | + |
28 | 33 | private MathFunctions() { |
29 | 34 | NoInstance.isPossible(); |
30 | 35 | } |
@@ -232,17 +237,15 @@ public static double rint(final double a) { |
232 | 237 | * @return rounded value |
233 | 238 | */ |
234 | 239 | @BasicFunction(classification = com.scriptbasic.classification.Math.class) |
235 | | - static public Object round(final double value, Integer numdecimalplaces) { |
| 240 | + static public Number round(final double value, Integer numdecimalplaces) { |
236 | 241 |
|
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 | + final BigDecimal bd = BigDecimal.valueOf(value); |
| 243 | + |
| 244 | + if (numdecimalplaces == null || numdecimalplaces == 0) { |
| 245 | + return bd.setScale(0, roundingMode).intValue(); |
242 | 246 | } else { |
243 | | - MathContext mc = new MathContext(numdecimalplaces, RoundingMode.HALF_EVEN); |
244 | | - BigDecimal result = bd.round(mc); |
245 | | - return result.doubleValue(); |
| 247 | + final MathContext mc = new MathContext(numdecimalplaces, roundingMode); |
| 248 | + return bd.round(mc).doubleValue(); |
246 | 249 | } |
247 | 250 | } |
248 | 251 |
|
|
0 commit comments