Solved

c# display timespan in 2 digits after decimal

Posted on 2011-02-28
8
1,890 Views
Last Modified: 2013-12-17
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
Comment
Question by:jtran007
8 Comments
 
LVL 9

Expert Comment

by:s_chilkury
ID: 34996557
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
 
LVL 13

Expert Comment

by:Naman Goel
ID: 34996571
String.Format("{0:0.##}", 123.4567);      // "123.46"
0
 
LVL 11

Accepted Solution

by:
Sudhakar Pulivarthi earned 125 total points
ID: 34996792
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 7

Expert Comment

by:jdavistx
ID: 34998730
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
 

Author Comment

by:jtran007
ID: 34998738
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34999105
Don't call "ToString()" on TotalMinutes.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34999107
...  or TotalSeconds    : )
0
 

Author Closing Comment

by:jtran007
ID: 35001356
Thanks,
JT
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question