-
-
Notifications
You must be signed in to change notification settings - Fork 421
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RoundingPrecisionSetting from #801
- Loading branch information
1 parent
583e57e
commit eb43316
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
src/main/java/net/wurstclient/settings/RoundingPrecisionSetting.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright (c) 2014-2024 Wurst-Imperium and contributors. | ||
* | ||
* This source code is subject to the terms of the GNU General Public | ||
* License, version 3. If a copy of the GPL was not distributed with this | ||
* file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt | ||
*/ | ||
package net.wurstclient.settings; | ||
|
||
import java.text.DecimalFormat; | ||
import java.text.DecimalFormatSymbols; | ||
import java.util.Locale; | ||
|
||
public final class RoundingPrecisionSetting extends SliderSetting | ||
{ | ||
private static final DecimalFormatSymbols SYMBOLS = | ||
new DecimalFormatSymbols(Locale.ENGLISH); | ||
|
||
private final DecimalFormat[] FORMATS; | ||
|
||
public RoundingPrecisionSetting(String name, String description, int value, | ||
int min, int max) | ||
{ | ||
super(name, description, value, min, max, 1, | ||
ValueDisplay.ROUNDING_PRECISION); | ||
|
||
if(min < 0) | ||
throw new IllegalArgumentException( | ||
"min must be greater than or equal to 0"); | ||
|
||
FORMATS = new DecimalFormat[max + 1]; | ||
} | ||
|
||
public DecimalFormat getFormat() | ||
{ | ||
int value = getValueI(); | ||
|
||
if(FORMATS[value] == null) | ||
{ | ||
String pattern = "0"; | ||
if(value > 0) | ||
pattern += "." + "#".repeat(value); | ||
|
||
FORMATS[value] = new DecimalFormat(pattern, SYMBOLS); | ||
} | ||
|
||
return FORMATS[value]; | ||
} | ||
|
||
public String format(double value) | ||
{ | ||
return getFormat().format(value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters