Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2479
  • Last Modified:

c# display timespan in 2 digits after decimal

Hi,
I got the elapse time from stopwatch, I'd like to display the elaspe time
in 2 digits after decimal point. What format I should use?
Thanks,
JT
0
jtran007
Asked:
jtran007
1 Solution
 
s_chilkuryCommented:
Try the following:
string timeElapsed =  String.Format("{0} Hours, {1} Minutes, {2} Seconds", Math.Floor(duration.TotalHours), Math.Floor(duration.TotalMinutes), Math.Floor(duration.TotalSeconds);
0
 
Naman GoelSoftware engineer 1Commented:
String.Format("{0:0.##}", 123.4567);      // "123.46"
0
 
Sudhakar PulivarthiProject Lead - EngineeringCommented:
Hi,

U can use substring instead, i think there is no format as such. we need to use regex.
string ealpsed = watch.Elapsed.Duration().ToString().Substring(0,11)
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
jdavistxCommented:
There's several ways you could do this, but here's the ones that jump to mind:

var sw = new Stopwatch();

sw.Start();
System.Threading.Thread.Sleep(1000);
sw.Stop();

Console.WriteLine(sw.Elapsed.TotalMilliseconds.ToString("N2"));
Console.WriteLine(Math.Round(sw.Elapsed.TotalMilliseconds, 2));
Console.WriteLine(String.Format("{0:0.00}", sw.Elapsed.TotalMilliseconds));

Open in new window

0
 
jtran007Author Commented:
Hi naman,

I use :
           MessageBox.Show("Time elapse: Min,Secs" + String.Format("{0:0.##}", ts.TotalMinutes.ToString()) + "," + String.Format("{0:0.##}", ts.TotalSeconds.ToString()));

But it is nor working.
Regards,
JT
0
 
käµfm³d 👽Commented:
Don't call "ToString()" on TotalMinutes.
0
 
käµfm³d 👽Commented:
...  or TotalSeconds    : )
0
 
jtran007Author Commented:
Thanks,
JT
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now