订购指数表示法值 [英] Ordering exponential notation values
问题描述
我有一个包含相关值的txt文件,例如:
ColumnOne,ColoumnTwo
1.1,0.1836856
3.3,-5.5
-0.38162,6.636666E-08
1.01516E-07,0.3695395
我想使用第二列订购这些值,然后将它们写入新的txt文件。
如何在不修改任何值的情况下执行此操作?
即如果原始文件包含6.636666E-08,那么新的txt文件需要包含该值而不是转换后的值,例如:0.00000006636666
谢谢。
[更新]
如何将文件读入元组?
谢谢
Hi,
I have a txt file containing related values such as these:
ColumnOne, ColoumnTwo
1.1, 0.1836856
3.3, -5.5
-0.38162, 6.636666E-08
1.01516E-07, 0.3695395
I would like to order these values using the second column then write them to a new txt file.
How can I do this without modifying any values ?
i.e. if the original file contains 6.636666E-08, then the new txt file needs to contain that value and not a converted value such as: 0.00000006636666
Thank you.
[Update]
How can read the file into a Tuple ?
Thank you
var reader = new StreamReader(File.OpenRead(@"C:\MyFile.txt"));
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
var val1 = decimal.Parse(values[0], System.Globalization.NumberStyles.Float);
var val2 = decimal.Parse(values[1], System.Globalization.NumberStyles.Float);
var val3 = values[2];
Tuple<decimal,> myTuple = Tuple.Create<decimal,decimal,string>(val1,val2, val3);
}</decimal,decimal,string></decimal,>
推荐答案
您可以使用LINQ对象,例如
You may employ LINQ to objects, e.g.
string filein = @"datain.txt";
var sorted = from line in File.ReadAllLines(filein)
let record = Tuple.Create<double, string>(double.Parse(line.Split(',')[1]), line)
orderby record.Item1
select record.Item2;
string fileout = @"dataout.txt";
File.WriteAllLines(fileout, sorted.ToArray());
干杯
Andi
Cheers
Andi
这是一个简化版本你的代码:
Here is a simplified version of your code:
var reader = new StreamReader(File.OpenRead(@"C:\MyFile.txt"));
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
var val1 = decimal.Parse(values[1], System.Globalization.NumberStyles.Float);
Tuple<decimal> myTuple = Tuple.Create<decimal,string>(val1,line);
}
// sort here on the decimal value
// write here result to text file
</decimal>
我认为 myTuple
应该是列表
Nota:我不是C#的专家。
I think myTuple
should be a list
Nota: I am not a specialist of C#.
这篇关于订购指数表示法值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!