How can I align console output in Kotlin? ```488 -...
# getting-started
v
How can I align console output in Kotlin?
Copy code
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
you can try use formatting like this: https://pl.kotl.in/gcsPhG1Mg
v
This points to a Hello World
v
Thank you
Didn't notice I can just use Javas String-formatter :D
d
best of both worlds 😃
v
Apparently
Copy code
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:
Copy code
number.toString().toBigInteger()
Works.. but seems rather inefficient.
Copy code
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
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
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
Sure, if that's fine by you... The programmer decides 😉
v
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?
112 Views