https://kotlinlang.org logo
Title
v

Valentin Metz

08/29/2020, 5:31 AM
How can I align console output in Kotlin?
488 -> 488	| 485/500
489 -> 44499	| 486/500
490 -> 70070	| 487/500
491 -> 8838	| 488/500
492 -> 3444	| 489/500
493 -> 7888	| 490/500
494 -> 494	| 491/500
496 -> 992	| 492/500
497 -> 994	| 493/500
480 -> 60000	| 477/500
460 -> 2202020	| 496/500
499 -> 499	| 497/500
498 -> 996	| 495/500
495 -> 990	| 494/500
483 -> 966	| 480/500
500 -> 500	| 498/500
290 -> 11011010	| 499/500
430 -> 11011010	| 500/500
d

deactivateduser

08/29/2020, 5:34 AM
you can try use formatting like this: https://pl.kotl.in/gcsPhG1Mg
v

Valentin Metz

08/29/2020, 5:36 AM
This points to a Hello World
v

Valentin Metz

08/29/2020, 5:37 AM
Thank you
Didn't notice I can just use Javas String-formatter :D
d

deactivateduser

08/29/2020, 5:40 AM
best of both worlds 😃
v

Valentin Metz

08/29/2020, 6:00 AM
Apparently
d != kotlin.ULong
Any idea what I'd use for unsigned longs?
So... apparently ULong can't print with %d... It also doesn't have a .toBigInteger()... So I need to:
number.toString().toBigInteger()
Works.. but seems rather inefficient.
print(
        String.format(
            "%-${range.toString().length}d -> %${ULong.MAX_VALUE.toString().length}d\t|\t%${range.toString().length}d / %d\n",
            number.toString().toBigInteger(),
            result.toString().toBigInteger(),
            counter.getAndIncrement(),
            range
        )
    )
d

deactivateduser

08/29/2020, 6:24 AM
Not sure if there is equivalent formatting for ulong as Java doesn't have unsigned type. I used to use
%ul
in C, but it apparently not working here. Probably other folks here know about it ;) You could probably just use %s instead of %d, hence
number.toString()
is enough, so there is no further conversion to big integer.
for the format string, probably if its possible, can be extracted out as another variable, to reduce the string interpolation processing in every repetition
v

Valentin Metz

08/29/2020, 7:19 AM
Yeah, in C/C++ I'd trust the compiler to get this, but in Java I wouldn't be sure about it.
On the other hand even doing this one million times wouldn't affect the runtime of the program much.
d

deactivateduser

08/29/2020, 7:24 AM
Sure, if that's fine by you... The programmer decides 😉
v

Valentin Metz

08/29/2020, 7:26 AM
In C I'd use a global for this. But I guess here I'd have no other choice than passing the variable at the function call?