bajro
bajro

Reputation: 1250

Scala get output in order

So this is a code output when I am doing some queries on my data :

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

You can see that the numbers are not printed in a nice way each one in the next line... So I want that these lines are always getting formatted when printed out like this:

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

See the difference? So I know that I have to take the length() of the previous one and something like this but I dont know how exactly?

Code snippet looks like this for the print statement:

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

Upvotes: 1

Views: 83

Answers (1)

dhg
dhg

Reputation: 52701

Probably you want this:

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))

The numbers in there (-10, 16) are used to tell the formatter the minimum amount of space that should be given for that item. Negative numbers mean "left-align"; positive numbers mean "right-align".

Lots more info can be found here: http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html

Upvotes: 3

Related Questions