Question

# My first application for reals!
set drink {{"Kiwi Juice"} {"Apple Juice"} {"Pumpkin Juice"} {"Soda"}}
set price {{1.50} {0.50} {2.00} {1.25}}
foreach c $drink b $price {
    set price1 [format "%-10s %+20s " "Drink" "Price"]

    set price2 [format "%-5s %+10d Dollars " $c $b]

    puts $price1
    puts $price2
}

It won't run. Apparently it always stops at the $c $b part, but I'm sure I did it right!

Était-ce utile?

La solution

The %d substitution in format, of which %+10d is a specialization, only works with integers. 1.50 isn't an integer, it's a floating-point number (or a string, of course). You probably want the %f substitution, specifically %+10.2f (2 decimal places, overall width 10, with sign):

set drink {{"Kiwi Juice"} {"Apple Juice"} {"Pumpkin Juice"} {"Soda"}}
set price {{1.50} {0.50} {2.00} {1.25}}
foreach c $drink b $price {
    set price1 [format "%-15s %+10s " "Drink" "Price"]
    set price2 [format "%-15s %+10.2f Dollars " $c $b]

    puts $price1
    puts $price2
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top