Skip to content

Commit 262edf1

Browse files
aakatz3jcxldn
andauthored
MathUtil, NumberUtil, StartupUtil Quick fixes (#141)
* Quick fixes * testing * Rollback UtilityClass, SuppressWarnings for consistency, * Rollback StartupUtil for consistency * NumberUtil - don't use UtilityClass for now * MathUtil - don't use UtilityClass for now Co-authored-by: James <[email protected]>
1 parent 3316338 commit 262edf1

File tree

5 files changed

+51
-8
lines changed

5 files changed

+51
-8
lines changed

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ plugins {
1111

1212
allprojects {
1313
group = "com.dumbdogdiner"
14-
version = "3.0.2"
14+
version = "3.0.3"
1515

1616
// java plugin is applied in subprojects
1717
apply plugin: "jacoco"

bukkit/src/main/java/com/dumbdogdiner/stickyapi/bukkit/util/SoundUtil.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ private static Boolean validate(CommandSender sender) {
3838
* @param pitch The pitch of the sound
3939
* @param delay T
4040
*/
41-
public static void queueSound(@NotNull Player player, @NotNull Sound sound, @NotNull float volume,
42-
@NotNull float pitch, @NotNull Long delay) {
41+
public static void queueSound(@NotNull Player player, @NotNull Sound sound, float volume, float pitch, long delay) {
4342
StickyAPI.getPool().submit(() -> {
4443
try {
4544
Thread.sleep(delay);
@@ -100,7 +99,7 @@ public static void sendSuccess(@NotNull Player player) {
10099
* @param type {@link NotificationType} The type of sound
101100
* @return {@link java.lang.Boolean}
102101
*/
103-
public static Boolean send(@NotNull CommandSender sender, @NotNull NotificationType type) {
102+
public static boolean send(@NotNull CommandSender sender, @NotNull NotificationType type) {
104103
if (!validate(sender)) {
105104
return false;
106105
}

common/src/main/java/com/dumbdogdiner/stickyapi/common/util/MathUtil.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import com.google.common.primitives.Ints;
99
import org.jetbrains.annotations.NotNull;
1010

11+
import java.math.BigDecimal;
12+
import java.math.RoundingMode;
1113
import java.text.CharacterIterator;
1214
import java.text.StringCharacterIterator;
1315
import java.util.List;
@@ -119,10 +121,16 @@ public static <T> T randomElement(@NotNull List<T> list) {
119121
* @return {@link Double}
120122
*/
121123
public static double round(double value, int places) {
122-
long factor = (long) Math.pow(10, places);
123-
value = value * factor;
124-
long tmp = Math.round(value);
125-
return (double) tmp / factor;
124+
return new BigDecimal(value).setScale(places, RoundingMode.HALF_UP).doubleValue();
125+
}
126+
127+
/**
128+
* Rounds a double to 2 decimals
129+
* @param d The double to round
130+
* @return The rounded double
131+
*/
132+
public static double round2Places(double d){
133+
return round(d, 2);
126134
}
127135

128136
/**

common/src/main/java/com/dumbdogdiner/stickyapi/common/util/NumberUtil.java

+25
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66

77
import org.jetbrains.annotations.NotNull;
88

9+
import java.math.BigDecimal;
10+
import java.math.RoundingMode;
11+
import java.text.DecimalFormat;
12+
import java.text.DecimalFormatSymbols;
13+
import java.text.NumberFormat;
14+
import java.util.Currency;
15+
import java.util.Locale;
16+
917
/**
1018
* <p>
1119
* Provides extra functionality for Java Number classes.
@@ -15,6 +23,11 @@ public final class NumberUtil {
1523
private NumberUtil() {
1624
}
1725

26+
private static final NumberFormat moneyFormat = NumberFormat.getCurrencyInstance();
27+
static {
28+
moneyFormat.setCurrency(Currency.getInstance(Locale.US));
29+
}
30+
1831
/**
1932
* <p>
2033
* Checks if the String contains only unicode digits. A decimal point is not a
@@ -137,4 +150,16 @@ public static int longToInt(@NotNull long lng) {
137150
}
138151
}
139152
}
153+
154+
155+
/**
156+
* Formats a price into US currency format
157+
* @param price Price to format
158+
* @return Formatted String
159+
*/
160+
public static String formatPrice(double price) {
161+
return moneyFormat.format(price);
162+
}
163+
164+
140165
}

common/src/test/java/com/dumbdogdiner/stickyapi/common/util/NumberUtilTest.java

+11
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,15 @@ public void testIntHelperMax() {
5858
public void testIntHelperMin() {
5959
assertEquals(NumberUtil.longToInt(((long) Integer.MIN_VALUE) - 1L), Integer.MIN_VALUE);
6060
}
61+
62+
@Test
63+
public void formatPrice() {
64+
System.out.println(NumberUtil.formatPrice(1.00001));
65+
assertEquals("$1.00", NumberUtil.formatPrice(1.0000001));
66+
}
67+
68+
@Test
69+
public void round2Places() {
70+
assertEquals(6.51D, MathUtil.round2Places(6.509));
71+
}
6172
}

0 commit comments

Comments
 (0)