문제

# 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!

도움이 되었습니까?

해결책

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
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top