Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2295
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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