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

2024-09-24 00:35:45 发布

您现在位置:Python中文网/ 问答频道 /正文

也许有人能告诉我这里发生了什么:

Python:

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

我正在分析温度数据,发现了一段我不懂的python代码。这是怎么回事?python是否将两个数字相加并将它们转换为int,然后除以10?你知道吗

C#可能看起来像:

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

但我不知道那是什么?数据是jibberish的,所以我不知道python中的这行到C#的正确翻译是什么


Tags: 数据代码数字温度tempinttemp2temp3
2条回答

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

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

will print marcog 42. Note that name is a string (%s) and number is an integer (%d for decimal).

See http://docs.python.org/library/stdtypes.html#string-formatting-operations for details.

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

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

Given format % values (where format is a string or Unicode object), % conversion specifications in format are replaced with zero or more elements of values. The effect is similar to the using sprintf() in the C language. If format is a Unicode object, or if any of the objects being converted using the %s conversion are Unicode objects, the result will also be a Unicode object.

可能需要设置一个python脚本并进行尝试,将您自己的值放入变量中。你知道吗

在C中,它看起来像:

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

或:

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

相关问题 更多 >