Skip to content

Commit

Permalink
Merge pull request #2860 from AnnieRuru/81-F_InsertComma
Browse files Browse the repository at this point in the history
update F_InsertComma to allow numbers bigger than INT_MAX
  • Loading branch information
MishimaHaruna authored Oct 19, 2020
2 parents f7f0c46 + e43983a commit 847755f
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions npc/other/Global_Functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,17 @@
// separator.
// callfunc "F_InsertComma",<number>{,<precision>,<separator>}
// Examples:
// callfunc("F_InsertComma",7777777) // returns "7,777,777"
// callfunc("F_InsertComma",1000000000,3,",") // returns "1,000,000,000"
// callfunc("F_InsertComma",1000000000,3,"_") // returns "1_000_000_000"
// callfunc("F_InsertComma",1000000000,4) // returns "10,0000,0000"
// callfunc("F_InsertComma", 7777777) // returns "7,777,777"
// callfunc("F_InsertComma", 1000000000, 3, ",") // returns "1,000,000,000"
// callfunc("F_InsertComma", 1000000000, 3, "_") // returns "1_000_000_000"
// callfunc("F_InsertComma", 1000000000, 4) // returns "10,0000,0000"
function script F_InsertComma {
.@value = getarg(0);
.@precision = getarg(1,3);
.@separator$ = getarg( 2,"," );

.@str$ = ""+.@value;
.@is_negative = ( .@value < 0 );

.@length = getstrlen( .@str$ ) - .@precision - .@is_negative;
while ( .@length > 0 ) {
.@str$ = insertchar( .@str$, .@separator$ , ( .@length + .@is_negative ) );
.@length -= .@precision;
}
.@str$ = getarg(0);
.@precision = getarg(1, 3);
.@separator$ = getarg(2, ",");
.@is_negative = (charat(.@str$, 0) == "-");
for (.@i = getstrlen(.@str$) - .@precision; .@i > .@is_negative; .@i -= .@precision)
.@str$ = insertchar(.@str$, .@separator$, .@i);
return .@str$;
}

Expand Down

0 comments on commit 847755f

Please sign in to comment.