简体   繁体   English

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

[英]Sum numbers in StreamWriter instead of writing next to each other in C#

I would like to write every data to a CSV file.我想将每个数据写入 CSV 文件。 Everything is working fine, but the numbers aren't adding up, they are written next to each other.一切正常,但数字并没有相加,它们是挨着写的。

This part of the code:这部分代码:

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);

Every property is a double, uint or int .每个属性都是double, uintint

What I expect: 1 + 3 = 4我的期望: 1 + 3 = 4

What I got: 1 + 3 = 13我得到了什么: 1 + 3 = 13

+ : mean a string concatenation, you can't calculate some of int inside string directly. + :表示字符串连接,不能直接计算字符串内部的一些 int 。
Try to use the following approach:尝试使用以下方法:

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);

You can change your code to:您可以将代码更改为:

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}");

I hope this help.我希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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