如何在UNIX中将字符串转换为整数 [英] How to convert string to integer in UNIX

查看:85
本文介绍了如何在UNIX中将字符串转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有d1="11"d2="07".我想将d1d2转换为整数并执行d1-d2.如何在UNIX中做到这一点?

I have d1="11" and d2="07". I want to convert d1 and d2 to integers and perform d1-d2. How do I do this in UNIX?

d1 - d2当前为我返回"11-07"作为结果.

d1 - d2 currently returns "11-07" as result for me.

推荐答案

标准解决方案:

 expr $d1 - $d2

您也可以这样做:

echo $(( d1 - d2 ))

,但是请注意,这会将07视为八进制数字! (因此077相同,但01010不同).

but beware that this will treat 07 as an octal number! (so 07 is the same as 7, but 010 is different than 10).

这篇关于如何在UNIX中将字符串转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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