Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚀 Add number format expression #4663

Closed
Closed
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
f1a8f7f
🚀 Add number format expression
AyhamAl-Ali Mar 15, 2022
c7b37d8
Little changes
AyhamAl-Ali Mar 15, 2022
cde535c
Prevent variables usage
AyhamAl-Ali Mar 15, 2022
ee12498
Support variables as custom format
AyhamAl-Ali Mar 15, 2022
f2348ed
Make it return null if format is invalid
AyhamAl-Ali Mar 16, 2022
df572d8
Add test script
AyhamAl-Ali Mar 16, 2022
05307fd
Update test script
AyhamAl-Ali Mar 16, 2022
66f12a4
Update test script
AyhamAl-Ali Mar 16, 2022
7184c95
Changes
AyhamAl-Ali Mar 16, 2022
dc61b39
Fix class to use same technique as #4664
AyhamAl-Ali Apr 21, 2023
cc78229
Merge branch 'master' into ench/formatted-number
AyhamAl-Ali Apr 21, 2023
8654a94
Apply suggestions from code review
AyhamAl-Ali Apr 22, 2023
f30aded
Add helpful comments and use lambda
AyhamAl-Ali Apr 22, 2023
b4077e8
Update src/main/java/ch/njol/skript/expressions/ExprFormatNumber.java
AyhamAl-Ali Mar 15, 2024
25d66c1
Change expression to function
AyhamAl-Ali Apr 5, 2024
ecd4a4a
Remove unused imports
AyhamAl-Ali Apr 5, 2024
0d5dc9a
Merge branch 'dev/feature' into ench/formatted-number
AyhamAl-Ali Apr 5, 2024
4488904
Refactoring and merge base, updating examples
AyhamAl-Ali Apr 5, 2024
664ebf1
return null instead of empty string
AyhamAl-Ali Apr 5, 2024
0ce55ab
Update examples
AyhamAl-Ali Apr 5, 2024
a04f476
Warn instead of erroring, but probably better to remove
AyhamAl-Ali Apr 5, 2024
f238bc0
Address reviews
AyhamAl-Ali Apr 6, 2024
80be1e0
better variable naming
AyhamAl-Ali Apr 6, 2024
ac9a7b4
Merge branch 'dev/feature' into ench/formatted-number
Moderocky Apr 12, 2024
9cdec48
Merge branch 'dev/feature' into ench/formatted-number
Moderocky Apr 13, 2024
926ee62
Merge branch 'dev/feature' into ench/formatted-number
Moderocky May 30, 2024
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
35 changes: 35 additions & 0 deletions src/main/java/ch/njol/skript/classes/data/DefaultFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.UUID;

Expand All @@ -50,6 +51,10 @@ public class DefaultFunctions {
private static String str(double n) {
return StringUtils.toString(n, 4);
}
private static final String DEFAULT_NUMBER_FORMAT_STRING = "###,###";
private static final DecimalFormat DEFAULT_NUMBER_FORMAT = new DecimalFormat(DEFAULT_NUMBER_FORMAT_STRING);
private static final ThreadLocal<DecimalFormat> NUMBER_FORMAT = ThreadLocal.withInitial(DecimalFormat::new);
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved


static {
Parameter<?>[] numberParam = new Parameter[] {new Parameter<>("n", DefaultClasses.NUMBER, true, null)};
Expand Down Expand Up @@ -568,6 +573,36 @@ public Boolean[] executeSimple(Object[][] params) {
}).description("Returns true if the input is NaN (not a number).")
.examples("isNaN(0) # false", "isNaN(0/0) # true", "isNaN(sqrt(-1)) # true")
.since("2.8.0");

Functions.registerFunction(new SimpleJavaFunction<String>("formatNumber", new Parameter[] {
new Parameter<>("number", DefaultClasses.NUMBER, true, null),
new Parameter<>("format", DefaultClasses.STRING, true, new SimpleLiteral<String>(DEFAULT_NUMBER_FORMAT_STRING, true))
}, DefaultClasses.STRING, true) {
@Override
public String[] executeSimple(Object[][] params) {
Number number = (Number) params[0][0];
String format = (String) params[1][0];

if (DEFAULT_NUMBER_FORMAT_STRING.equals(format)) // shortcut
return CollectionUtils.array(DEFAULT_NUMBER_FORMAT.format(number));

try {
DecimalFormat numberFormat = NUMBER_FORMAT.get();
numberFormat.applyPattern(format);
return CollectionUtils.array(numberFormat.format(number));
} catch (IllegalArgumentException e) {
// Skript.warning("Invalid number format: " + format); // TODO find a better solution for such warnings/errors that doesn't spam the console
return null;
}
}
}).description("Converts numbers to human-readable format. By default, '###,###' (e.g. '123,456,789') will be used. For reference, see this "
+ "<a href=\"https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html\" target=\"_blank\">article</a>.")
.examples(
"command /formatnumber <number>:",
"\taliases: fn",
"\ttrigger:",
"\t\tsend \"Formatted: %formatNumber(arg-1)%\" to sender"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some more examples of how to use DecimalFormat would be nice. The table on the javadocs is good, but pretty dense for new users.

).since("INSERT VERSION");
}

}
17 changes: 17 additions & 0 deletions src/test/skript/tests/regressions/4663-formatted numbers.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
test "formatted numbers function":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure to test invalid numbers too! NaN, infinity, null values, etc.
Floating point would also be good to show.

set {_num1} to formatNumber(123456789)
assert {_num1} = "123,456,789" with "default number format failed ##1"

set {_num2} to formatNumber(1234567)
assert {_num2} = "1,234,567" with "default number format failed ##2"

set {_num3} to formatNumber(12345678, "##,##.00")
assert {_num3} = "1,2,3,4,5,6,7,8.00" with "custom number format failed ##1"

set {_cFormat} to "####,####"
set {_num4} to formatNumber(12345678, {_cFormat})
assert {_num4} = "12,34,56,78" with "custom number format failed ##2"

set {_cFormat2} to "##,.##"
set {_num5} to formatNumber(12345678, {_cFormat2})
assert {_num5} is not set with "custom number format failed ##3"
Loading