Generating a table with columns of the same width (displayed on the console in Xcode)

This is the part of the code that I use to check my calculations, I just write these values ​​in the console in Xcode. Each of the arrays is declared with the values ​​shown below.

var water_deficit: [Int] = []

The program calculates the values ​​for water scarcity and adds them to this list (calculations not shown)

let months = ["January","Feburary","March","April","May","June","July","August","September","October","November","December"]
let rainfall = [38,94,142,149,236,305,202,82,139,222,178,103]
let raindays = [3,6,8,7,12,16,10,8,12,14,11,7]
for i in 0...11 {
    println("\(months[i]) \t \(rainfall[i]) \t \(raindays[i]) \t \(water_deficit[i])")
}

Output as shown on console:

Month    Rainfall    Raindays    Water Deficit
January      38      3   38
Feburary     94      6   -18
March    142     8   -8
April    149     7   -1
May      236     12      116
June     305     16      301
July     202     10      202
August   82      8   82
September    139     12      101
October      222     14      203
November     178     11      208
December     103     7   103

As you can see, since the length of the words / numbers is different, the columns are offset. What do I need to do to create columns of a certain width to avoid this problem?

+4
source share
4 answers

Try the following:

for i in 0...11 {
  let month = (months[i] as NSString).UTF8String
  println(String(format:"%-10s %10d %10d %10d",  month, 
                 rainfall[i], raindays[i], water_deficit[i]))
}

For more information on formats and format specifiers, see here and here .

let month = (months[i] as NSString).UTF8String String C String: a C String, , String ( , ).

+5

, , , . . (Swift 2.0, Xcode 7 beta3).

  let headings = ["Month    ", "Rainfall", "RainDays", "Water Deficit"]

let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]

let rainFalls = [38, 94, 142,149,236,305,202, 82, 139, 222, 178, 103]
let rainyDays = [3, 6, 8,7,12,16,10, 8, 12, 14, 11, 7]
let waterDeficits = [38, -18, -8,-1,116,301,202, 82, 101, 203, 208, 103]


func getRightJustifiedStringRepFor(number: Int, refString:String) -> String
{
    let length = refString.utf8.count
    let stringRep = String(number)

    var paddedStringRep : String = ""

    //Build necessary padding
    for  var i = 0 ; i <  (length - stringRep.utf8.count) ; i++
    {
        paddedStringRep += " "
    }

    paddedStringRep += stringRep

    return paddedStringRep
}


let headingsToDisplay = headings.reduce(""){

    (var accummulated : String, item: String) -> String in
    return accummulated  + item +  "\t\t\t"

}

print(headingsToDisplay)

//Get proper aligned months with forward padding as we want them left aligned
let leftJustifiedMonths = months.map{
    (var item: String) -> String in
    let paddingsNeeded = 9 - item.utf8.count  //9 is the  length of lengthy month name
    for var i = 0 ; i < paddingsNeeded ; i++
    {
        item += " "
    }
    return item
}

print("\n")

for i in 0...11
{
    print(leftJustifiedMonths[i], appendNewline:false)
    print("\t\t\t", appendNewline:false)
    print( (getRightJustifiedStringRepFor(rainFalls[i], refString: "Rainfall")), appendNewline:false)
    print("\t\t\t", appendNewline:false)
    print( (getRightJustifiedStringRepFor(rainyDays[i], refString: "RainDays")),appendNewline:false)
    print("\t\t\t", appendNewline:false)
    print( (getRightJustifiedStringRepFor(waterDeficits[i], refString: "Water Deficit")),appendNewline:false)

    print("\n")

}

:

enter image description here

+2

- , , \t .

, :

  • → ,

    let months: [String] = [ "jan", "feb", "mar", "apr", "may", "june", "july", "aug", "sept", "oct", "", "" ]

  • , :

    let nf = NSNumberFormatter () nf.numberStyle = NSNumberFormatterStyle.DecimalStyle

0
source
for i in 0...11 {
print(months[i])
countElements(months[i]) > 4 ? print("\t\t") : print("\t\t\t")

print(rainfall[i])
countElements(String(rainfall[i])) > 4 ? print("\t\t") : print("\t\t\t")

print(raindays[i])
countElements(String(raindays[i])) > 4 ? print("\t\t") : print("\t\t\t")

print(water_deficit[i])
print("\n")

}

0
source

Source: https://habr.com/ru/post/1599512/


All Articles