[英]Sum numbers in StreamWriter instead of writing next to each other in C#
我想将每个数据写入 CSV 文件。 一切正常,但数字并没有相加,它们是挨着写的。
这部分代码:
file.WriteLine("\"1. koordinata:\"" + ";" + opening.rect_co_x + ";" + cord.y + ";" + opening.rect_co_z);
file.WriteLine("\"2. koordinata:\"" + ";" + opening.rect_co_x + opening.rect_width + ";" + cord.y + ";" + opening.rect_co_z);
每个属性都是double, uint
或int
。
我的期望: 1 + 3 = 4
我得到了什么: 1 + 3 = 13
+
:表示字符串连接,不能直接计算字符串内部的一些 int 。
尝试使用以下方法:
int a = 1, b = 3;
string str = $"a;{a + b};b"; // a;4;b
//or string.Format
string str2 = string.Format("a;{0};b", a + b);
您可以将代码更改为:
file.WriteLine($"\"1. koordinata:\";{opening.rect_co_x };{cord.y};{opening.rect_co_z}");
file.WriteLine($"\"2. koordinata:\";{opening.rect_co_x + opening.rect_width};{cord.y};{opening.rect_co_z}");
我希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.