-1

I measure the Time of a wav File and got it back in a TimeSpan. When I look into the Timespan the totalSeconds value is the exact time i need! For example: TotalSeconds = 6.6999999999999993 When I write it into a File, it will be roundet to 6.7!

I need the exact value in the textfile!

Here is the code:

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>");
}
J. Steen
  • 15,470
  • 15
  • 56
  • 63
  • ts.TotalSeconds.ToString() gives you the rounded off answer? – Anurag Feb 13 '15 at 09:14
  • How do you write it to file exactly? Show the relevant code as well. – Soner Gönül Feb 13 '15 at 09:14
  • Impossible to help if you don't post the code you used. What format string did you use? How did you write it to the file? – Panagiotis Kanavos Feb 13 '15 at 09:14
  • http://en.wikipedia.org/wiki/Femtosecond – Hans Passant Feb 13 '15 at 09:16
  • 1
    Moreover, TimeSpan supports only up to 10^7 ticks in a second. It can't represent the accuracy you posted. How did you get that value? At best there's a rounding error and 6.7 is the real value. – Panagiotis Kanavos Feb 13 '15 at 09:22
  • To quote from the Wikipedia page @HansPassant linked to: "A ray of light travels approximately 0.3 µm (micrometers) in 1 femtosecond, a distance comparable to the diameter of a virus." Why is that kind of precision relevant to your problem? – heijp06 Feb 13 '15 at 09:50
  • It is relevant to me, because the app which is reading this file is not able to read the wav when the time is the wrong! – user1688566 Feb 13 '15 at 10:01

2 Answers2

0

You can simply write the value of TotalSeconds to a file, e.g.:

TimeSpan ts = new TimeSpan(1234);
File.WriteAllText(@"C:\Temp\TimeTest.txt", ts.TotalSeconds.ToString());
// File contains: 0.0001234
Alan
  • 2,962
  • 2
  • 15
  • 18
0

You can pass the parameters to the ToString method:

ts.TotalSecondsToString("0.00"); //2dp Number

ts.TotalSecondsToString("n2"); // 2dp Number

ts.TotalSecondsToString("c2"); // 2dp currency

The first two options are for your issue.

Piero Alberto
  • 3,823
  • 6
  • 56
  • 108