繁体   English   中英

在 StreamWriter 中对数字求和,而不是在 C# 中彼此相邻写入

[英]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, uintint

我的期望: 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM