TCL格式化困境

时间:2014-04-18 06:11:17

标签: tcl

# 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部分,但我确信我做得对!

1 个答案:

答案 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
}