Tcl 中是否有 'string 与 X' 的等价物?在 C stdlib.h 中找到的函数? [英] Is there an equivalent in Tcl of 'string to X' functions found in C stdlib.h?

查看:17
本文介绍了Tcl 中是否有 'string 与 X' 的等价物?在 C stdlib.h 中找到的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C 的 stdlib.h 中有诸如 atofatoi 之类的标准函数,用于将字符串转换为浮点数/整数(并执行也反转).Tcl 中是否有这样的等价物,或者我是否需要编写自己的流程来执行这些任务?

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 too). Is there an equivalent of this in Tcl or do I need to write my own process for carrying out these tasks?

推荐答案

在 Tcl 中一切都是字符串,但函数需要一个数字(如 expr)将使用该字符串"作为整数:

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

如果您想以特定方式格式化数字(例如生成特定精度的浮点数),则可以使用 format:

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

这篇关于Tcl 中是否有 'string 与 X' 的等价物?在 C stdlib.h 中找到的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆