Stuart Harrell
Stuart Harrell

Reputation: 127

Printing with variable length width

In C, I could write,

int width = 5;
int number = 10;
printf("%*d", width, number); //output: "   10"

I see that Scala could do,

val number = 10
print(f"$number%5d") //output: "   10"

But I would like to pass the width in as a variable, is this possible?

Upvotes: 3

Views: 573

Answers (1)

yǝsʞǝla
yǝsʞǝla

Reputation: 16422

I can only offer an alternative:

val number = 10
val width = 5
printf(s"%${width}d", number)

It seems that there is no exact equivalent of C printf in Scala.

Upvotes: 5

Related Questions