Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/main/java/ch/njol/skript/classes/data/JavaClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public class JavaClasses {

public static final int VARIABLENAME_NUMBERACCURACY = 8;

/**
* The pattern for scientific notation.
* <p>
* The pattern is the letter {@code e} or {@code E} followed by a sign and one or more digits.
* </p>
*/
public static final String SCIENTIFIC_PATTERN = "(?:[eE][+-]?\\d+)?";

/**
* The format of an integer.
* <p>
Expand All @@ -49,8 +57,8 @@ public class JavaClasses {
* </p>
*/
public static final Pattern INTEGER_PATTERN =
Pattern.compile("(?<num>" + INTEGER_NUMBER_PATTERN + ")" +
"(?: (?:in )?(?:(?<rad>rad(?:ian)?s?)|deg(?:ree)?s?))?");
Pattern.compile("(?<num>%s%s)(?: (?:in )?(?:(?<rad>rad(?:ian)?)|deg(?:ree)?)s?)?"
.formatted(INTEGER_NUMBER_PATTERN, SCIENTIFIC_PATTERN));

/**
* The format of a decimal number.
Expand All @@ -74,8 +82,8 @@ public class JavaClasses {
* </p>
*/
public static final Pattern DECIMAL_PATTERN =
Pattern.compile("(?<num>" + DECIMAL_NUMBER_PATTERN + ")" +
"(?: (?:in )?(?:(?<rad>rad(?:ian)?s?)|deg(?:ree)?s?))?");
Pattern.compile("(?<num>%s%s)(?: (?:in )?(?:(?<rad>rad(?:ian)?)|deg(?:ree)?)s?)?"
.formatted(DECIMAL_NUMBER_PATTERN, SCIENTIFIC_PATTERN));

static {
Classes.registerClass(new ClassInfo<>(Object.class, "object")
Expand All @@ -94,7 +102,7 @@ public class JavaClasses {
"Please note that many expressions only need integers, i.e. " +
"will discard any fractional parts of any numbers without producing an error.",
"Radians will be converted to degrees.")
.usage("[-]###[.###] [[in ](rad[ian][s]|deg[ree][s])]</code> (any amount of digits; very large numbers will be truncated though)")
.usage("[-]###[.###] [e[+|-]###] [[in ](rad[ian][s]|deg[ree][s])]")
.examples(
"set the player's health to 5.5",
"set {_temp} to 2*{_temp} - 2.5",
Expand Down
11 changes: 11 additions & 0 deletions src/test/skript/tests/misc/scientific notation.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
test "scientific notation":
assert 1.23e+4 is 12300 with "1.23e4 is not 12300"
assert 1.23e-4 is 0.000123 with "1.23e-4 is not 0.000123"
assert 1.23E+4 is 12300 with "1.23E4 is not 12300"
assert 1.23E-4 is 0.000123 with "1.23E-4 is not 0.000123"

assert 123e+2 is 12300 with "123e2 is not 12300"
assert 123e-2 is 1.23 with "123e-2 is not 1.23"

assert 1.23e+4 degs is 12300 with "1.23e+4 degs is not 12300"
assert 1.23e4 degs is 12300 with "1.23e4 degs is not 12300"