# 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
}
它不会运行。显然它始终停留在$c $b
部分,但我确信我做得对!
答案 0 :(得分:1)
%d
中的format
替换,其中%+10d
是专业化,仅适用于整数。 1.50
不是整数,它是一个浮点数(当然是一个字符串)。您可能需要%f
替换,特别是%+10.2f
(2位小数,总宽度10,带符号):
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
}