Muhammad Azeem
Muhammad Azeem

Reputation: 479

Convert Textvalue String(Seconds) Value into Double (Second) using TimeSpan

Am going to Convert a string value into double... Actually I want to convert Seconds into Mintues using TimeSpan Value that i want to convert is 780.0000000656558 having 780 Seconds and remaining are millisecond's... i want to display 13.00000000010942633333 or for example 13mintues in case if there is no accurate perception

Am trying to using following Code

if (DownTime != null)
{
    DownTime.Text = sensor.DownTime;
    if (DownTime.Text.Length > 0)
    {
         //TimeSpan.FromSeconds(DownTime.Text).ToString()
         double DownTimeSeconds = Convert.ToDecimal(DownTime.Text).ToString();
         double DownTimeMints = DownTimeSeconds / 60;
         sensor.DownTime = Convert.ToString(DownTimeMints);
         DownTime.Text = sensor.DownTime;
         DownTime.ToolTip = DownTime.Text;
         if (DownTime.Text.Length >= 25)
         {
             DownTime.Text = DownTime.Text.Substring(0, 20) + "...";
         }
    }
}

i want to show output in Minutes... What Should i do Thanks in Advance

Upvotes: 1

Views: 1075

Answers (2)

muhammad kashif
muhammad kashif

Reputation: 2624

This code is giving me 13 minutes: try at your end please.:

        double time = 780.0000000656558;
        TimeSpan ts = TimeSpan.FromSeconds(time);
        Console.WriteLine(ts.Minutes);
        //Console.WriteLine(ts.TotalMinutes);  // this will give u mintes.fractionalpart
        Console.ReadLine();

Upvotes: 1

Richard
Richard

Reputation: 108975

TimeSpan.FromSeconds takes a double and includes the fractional parts (adding 0.1 here to make the fraction clear):

Using PowerShell (which automatically does the string to double conversion)

PS [64]> [TimeSpan]::FromSeconds("780.1000000656558")


Days              : 0
Hours             : 0
Minutes           : 13
Seconds           : 0
Milliseconds      : 100
Ticks             : 7801000000
TotalDays         : 0.00902893518518519
TotalHours        : 0.216694444444444
TotalMinutes      : 13.0016666666667
TotalSeconds      : 780.1
TotalMilliseconds : 780100

However as the commenter noted there is a limit to the resolution of TimeSpan: ticks, or 100ns intervals. Your fractional part is smaller than that. Thus in the example about the Ticks includes seven decimal places, but your first non-zero fraction digit is in position eight.

This can be seen by using TimeSpan.FromTicks, manually converting the string to a long, for the same time (minutes, seconds & milliseconds), but that first non-zero decimal digit is still lost:

PS [64]> [TimeSpan]::FromTicks(7801000000)


Days              : 0
Hours             : 0
Minutes           : 13
Seconds           : 0
Milliseconds      : 100
Ticks             : 7801000000
TotalDays         : 0.00902893518518519
TotalHours        : 0.216694444444444
TotalMinutes      : 13.0016666666667
TotalSeconds      : 780.1
TotalMilliseconds : 780100

Upvotes: 2

Related Questions