2013-10-12 143 views
3

在C中,可以寫,打印長度可變寬度

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

我看到的Scala可以做,

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

但我想傳遞的寬度作爲變量,是這可能嗎?

+0

http://stackoverflow.com/questions/3989243/how-to-format-strings-in-scala – Ashalynd

回答

3

我只能提供一種替代方案:

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

似乎有是Scala C的printf沒有完全等效。