Skip to content

Commit 75a361e

Browse files
committed
add format example
1 parent 8435ba7 commit 75a361e

File tree

4 files changed

+15
-23
lines changed

4 files changed

+15
-23
lines changed

libCompiler/src/main/assets/code_sample/sysutils/Formatter.pas

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,11 @@
66
s : string;
77
begin
88
// Explicit argument indices may be used to re-order output.
9-
s := format('%4$2s %3$2s %2$2s %1$2s', 'a', 'b', 'c', 'd');
9+
s := format('%4$2s %3$2s %2$2s %1$2s', ['a', 'b', 'c', 'd']);
1010
// -> ' d c b a'
1111
WriteLn(s);
1212

13-
s := format('pi = %+10.4f', pi);
13+
s := format('pi = %+10.4f', [pi]);
1414
// -> 'e = +2,7183'
1515
WriteLn(s);
16-
17-
// The '(' numeric flag may be used to format negative numbers with
18-
// parentheses rather than a minus sign. Group separators are
19-
// automatically inserted.
20-
s := format('Amount gained or lost since last statement: $ %(,.2f',
21-
balanceDelta);
22-
// -> 'Amount gained or lost since last statement: $ (6,217.58)'
23-
writeln(s);
2416
end.

libCompiler/src/main/java/com/duy/pascal/backend/builtin_libraries/SysUtilsLibrary.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,12 @@ public StringBuilder AnsiLowerCase(StringBuilder s1) throws EConvertError {
282282

283283

284284
@PascalMethod(description = "Format a string with given arguments. (Java)")
285-
public StringBuilder format(String format, Object[] arg) {
286-
return new StringBuilder(String.format(format, arg));
285+
public StringBuilder format(StringBuilder format, Object[] arg) {
286+
return new StringBuilder(String.format(format.toString(), arg));
287287
}
288288

289289
@PascalMethod(description = "Convert a TDateTime time to a string using a predefined format")
290-
public StringBuilder TimeToStr(Long time) {
290+
public StringBuilder timeToStr(Long time) {
291291
Date date = new Date(time);
292292
return new StringBuilder(date.toString());
293293
}

libCompiler/src/test/java/com/duy/pascal/lib/SysUtilsTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,12 @@ public void testAll() {
3737
public void testTime() {
3838
run("time.pas");
3939
}
40+
41+
public void testFormatArg() {
42+
run("test_format_arg.pas");
43+
}
44+
45+
public void testFormatArg2() {
46+
run("Formatter.pas");
47+
}
4048
}

test_pascal/test_libraries/sysutils/Formatter.pas

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,11 @@
66
s : string;
77
begin
88
// Explicit argument indices may be used to re-order output.
9-
s := format('%4$2s %3$2s %2$2s %1$2s', 'a', 'b', 'c', 'd');
9+
s := format('%4$2s %3$2s %2$2s %1$2s', ['a', 'b', 'c', 'd']);
1010
// -> ' d c b a'
1111
WriteLn(s);
1212

13-
s := format('pi = %+10.4f', pi);
13+
s := format('pi = %+10.4f', [pi]);
1414
// -> 'e = +2,7183'
1515
WriteLn(s);
16-
17-
// The '(' numeric flag may be used to format negative numbers with
18-
// parentheses rather than a minus sign. Group separators are
19-
// automatically inserted.
20-
s := format('Amount gained or lost since last statement: $ %(,.2f',
21-
balanceDelta);
22-
// -> 'Amount gained or lost since last statement: $ (6,217.58)'
23-
writeln(s);
2416
end.

0 commit comments

Comments
 (0)