Is there an equivalent in Tcl of 'string to X' functions found in C stdlib.h?

后端 未结 5 1478
南旧
南旧 2021-01-19 17:50

There are standard functions such as atof and atoi in C\'s stdlib.h for converting strings to floats / integers (and to do the reverse

5条回答
  •  醉酒成梦
    2021-01-19 18:45

    Everything is a string in Tcl, but functions that expect a number (like expr) will use that 'string' as an integer:

    % set str " 123 "
     123
    % set num [expr $str*2]
    246
    

    If you want to format a number in a specific way (like producing a floating a point number of a specific precision) then you can use format:

    % set str " 1.234 "
     1.234
    % set fnum [format "%.2f" $str]
    1.23
    

提交回复
热议问题