Skip to content

Commit cc76fa6

Browse files
committedDec 30, 2024
add round with precision
1 parent e3bcbcd commit cc76fa6

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ lang: en
66

77
## 4.15.2
88

9+
- 🚀 Feat: add Operators.round(value, precision) to round values. `Operators.round(2.3456, 2)` will return `2.35`.
910
- 🐛 Fix: Random.randomInt not return the maximum value.
1011

1112
## 4.15.1

‎src/org/openpatch/scratch/Operators.java

+11
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,17 @@ public static final int round(double x) {
154154
return (int) Math.round(x);
155155
}
156156

157+
/**
158+
* Rounds a number to a specified number of decimal places.
159+
* @param x a value
160+
* @param precision the number of decimal places to round to
161+
* @return the rounded value
162+
*/
163+
public static final double round(double x, int precision) {
164+
double scale = Math.pow(10, precision);
165+
return Math.round(x * scale) / scale;
166+
}
167+
157168
/**
158169
* "mod" is an abbreviation for "modulo". Modulo returns the remainder when the first input is
159170
* divided by the second input.

0 commit comments

Comments
 (0)