需要帮助将python中的表达式转换为C#

时间:2014-07-03 10:18:57

标签: c# python

也许某个阵营中的某个人可以告诉我这里发生了什么:

的Python:

temp = int('%d%d' % (temp2, temp3)) / 10.0;

我正在解析温度数据,发现了一条我无法理解的python。这里发生了什么? python是否在这里将两个数字相加并将它们转换为int,然后除以10?

C#可能看起来像:

temp = ((int)(temp2+temp3))/10;

但是我不确定%有什么作用?数据是乱七八糟的,所以我不知道在python到C#

中该行的正确翻译是什么

2 个答案:

答案 0 :(得分:1)

在C#中它看起来像:

var temp = int.Parse(temp2.ToString() + temp3.ToString())/10f;

或:

var temp = Convert.ToInt32(string.Format("{0}{1}", temp2, temp3))/10f;

答案 1 :(得分:0)

这类似:What's the difference between %s and %d in Python string formatting?

name = 'marcog'
number = 42
print '%s %d' % (name, number)
  

将打印marcog 42.请注意,name是一个字符串(%s),number是一个整数(%d表示十进制)。

     

请参阅   http://docs.python.org/library/stdtypes.html#string-formatting-operations   详情。

所以看起来“%”只是告诉python将右边的值放到左边的占位符中。

来自我引用的答案中链接的文档:

  

给定格式%值(其中format是字符串或Unicode对象),格式中的%转换规范将替换为零个或多个值元素。效果类似于在C语言中使用sprintf()。如果format是Unicode对象,或者使用%s转换转换的任何对象是Unicode对象,则结果也将是Unicode对象。

可能想要设置一个python脚本并尝试一下,将自己的值放入变量中。