Scala 按顺序获取输出

Scala get output in order

所以这是我对数据进行一些查询时的代码输出:

Quartal    Cooperation    Sponsoring        Fees
2014/2    52.878.990,22    1.347.863,90    147.400.000,00
2013/1    41.915.908,42    18.867.625,26    153.753.810,00
2013/2    58.940.141,17    9.196.517,72    153.855.201,00
2014/1    39.257.778,34    17.836.682,47    147.269.362,00
Total:    192.992.818,15    47.248.689,35    602.278.373,00

您可以看到数字在下一行中的每一行都没有以很好的方式打印...所以我希望这些行在打印时总是被格式化:

Quarter     Cooperation      Sponsoring            Fees
2014/2    52.728.402,92    1.347.863,90  147.400.000,00
2014/3    37.136.924,43    8.270.494,07  147.800.000,00
2014/4    63.044.689,86    1.318.163,94  146.900.000,00
Total    152.910.017,21   10.936.521,91  442.100.000,00

看出区别了吗?所以我知道我必须采用前一个的 length() 和类似的东西,但我不知道具体怎么做?

打印语句的代码片段如下所示:

    println("%s    %s    %s        %s".format("Quartal","Cooperation","Sponsoring","Fees"))
    println(s"Total:    %,.2f    %,.2f    %,.2f".format(totalMedKF2.sum,totalMedKF4.sum,totalMedKF31.sum))

可能你想要这个:

println("%-10s %16s %16s %16s".format("Quartal","Cooperation","Sponsoring","Fees"))
println(s"Total:     %,16.2f %,16.2f %,16.2f".format(totalMedKF2.sum,totalMedKF4.sum,totalMedKF31.sum))

其中的数字 (-10, 16) 用于告诉格式化程序应为该项目提供的最小 space 数量。负数表示"left-align";正数表示 "right-align".

更多信息可以在这里找到:http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html