Konrad
Konrad

Reputation: 776

How to transform milliseconds into seconds .?

I have to transform my Stopwatch "Variable" into seconds ?

Stopwatch czasAlg = new Stopwatch(); 
czasAlg.Start();
//Do semothing
czasAlg.Stop();
Decimal Tn = czasAlg.ElapsedMilliseconds/(decimal)n;

Upvotes: 18

Views: 52067

Answers (8)

sunnamed
sunnamed

Reputation: 233

Instead of using math and multiplying/diving like this: seconds (60) * 1000 = 60000, use TimeSpan instead, it's using bit operations, and due to it has a minimum cost of performance.

Milliseconds to seconds

int sixtyThousandsMillisecondsInSeconds = (int)TimeSpan.FromMilliseconds(60000).TotalSeconds;
// Outputs 60

Seconds to milliseconds

// 1 min (60 seconds) in milliseconds = 60000 (i.e 60 * 1000)
int sixtySecondsInMilliseconds = (int)TimeSpan.FromSeconds(60).TotalMilliseconds;
// Outputs 60000

Using LINQPad

int sixtyThousendsMillisecondsInSeconds = (int)TimeSpan.FromMilliseconds(60000).TotalSeconds;
sixtyThousendsMillisecondsInSeconds.Dump();

int sixtySecondsInMilliseconds = (int)TimeSpan.FromSeconds(60).TotalMilliseconds;
sixtySecondsInMilliseconds.Dump();

Results

60

60000

Upvotes: 0

Oleg
Oleg

Reputation: 289

Without your own constants and magic numbers:

TimeSpan.FromMilliseconds(x).TotalSeconds

Upvotes: 28

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272802

Just to be different:

Multiply by 0.001.

Upvotes: 25

ThiefMaster
ThiefMaster

Reputation: 318798

Divide by 1000.

Upvotes: 1

marcind
marcind

Reputation: 53191

Use

czasAlg.Elapsed.TotalSeconds

Upvotes: 8

Vlad
Vlad

Reputation: 35594

Divide by 1000.0?

Upvotes: 0

Joey
Joey

Reputation: 354864

Divide by 1000 or use

czasAlg.Elapsed.TotalSeconds

Upvotes: 48

Related Questions