如何正确对齐输出 [英] How to align output correctly
本文介绍了如何正确对齐输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下价值:
val x = List((("Burger and chips",4.99),4.99,1), (("Pasta & Chicken with Salad",8.99), 8.99,2), (("Rice & Chicken with Chips",8.99), 8.99,2))
打印后我得到这个:
x.foreach(x => println(x._3 + " x " + x._1 +"\t\t\t\t"+"$"+x._2 * x._3 ))
1 x (Burger and chips,4.99) $4.99
2 x (Pasta & Chicken with Salad,8.99) $17.98
2 x (Rice & Chicken with Chips,8.99) $17.98
但是我想要这个结果:
1 x (Burger and chips,4.99) $4.99
2 x (Pasta & Chicken with Salad,8.99) $17.98
2 x (Rice & Chicken with Chips,8.99) $17.98
我知道文字大小是导致问题的原因,但有办法解决吗??
I know the text size is causing the problem but is there a way around it??
谢谢
推荐答案
Scala 的 "f interpolator" 对此很有用:
Scala's "f interpolator" is useful for this:
x.foreach {
case (text, price, amount) => println(f"$amount x $text%-40s $$${price*amount}")
}
// prints:
1 x (Burger and chips,4.99) $4.99
2 x (Pasta & Chicken with Salad,8.99) $17.98
2 x (Rice & Chicken with Chips,8.99) $17.98
这篇关于如何正确对齐输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文