我测量wav文件的时间并将其恢复到TimeSpan中。 当我查看Timespan时,totalSeconds值是我需要的确切时间! 例如:TotalSeconds = 6.6999999999999993 当我把它写入文件时,它将会转到6.7!
我需要文本文件中的确切值!
以下是代码:
TimeSpan duration = GetWavFileDuration(wav);
string[] wavStrings = wav.Split('\\');
using (TextWriter writer = new StreamWriter(wav.Replace(".wav", ".plist"), false, Encoding.UTF8))
{
writer.NewLine = "\n";
writer.Write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" +
"<plist version=\"1.0\">\n" +
"<dict>\n" +
"\t<key>bundle_id</key>\n" +
"\t<string>" + folderStrings[folderStrings.Length - 1] + "</string>\n" +
"\t<key>channels</key>\n" +
"\t<integer>2</integer>\n" +
"\t<key>duration</key>\n" +
"\t<real>" + duration.TotalSeconds.ToString().Replace(',', '.') + "</real>\n" +
"\t<key>filetitle</key>\n" +
"\t<string>" + wavStrings[wavStrings.Length - 1] + "</string>\n" +
"\t<key>sender</key>\n" +
"\t<string>" + folderStrings[folderStrings.Length - 1] + "</string>\n" +
"</dict>\n" +
"</plist>");
}
答案 0 :(得分:0)
您可以简单地将TotalSeconds的值写入文件,例如:
TimeSpan ts = new TimeSpan(1234);
File.WriteAllText(@"C:\Temp\TimeTest.txt", ts.TotalSeconds.ToString());
// File contains: 0.0001234
答案 1 :(得分:0)
您可以将参数传递给ToString方法:
ts.TotalSecondsToString("0.00"); //2dp Number
ts.TotalSecondsToString("n2"); // 2dp Number
ts.TotalSecondsToString("c2"); // 2dp currency
前两个选项适用于您的问题。