Solved

c# display timespan in 2 digits after decimal

Posted on 2011-02-28
8
1,842 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
Comment Utility
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
Comment Utility
String.Format("{0:0.##}", 123.4567);      // "123.46"
0
 
LVL 11

Accepted Solution

by:
Sudhakar Pulivarthi earned 125 total points
Comment Utility
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
 
LVL 7

Expert Comment

by:jdavistx
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:jtran007
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Don't call "ToString()" on TotalMinutes.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
...  or TotalSeconds    : )
0
 

Author Closing Comment

by:jtran007
Comment Utility
Thanks,
JT
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Hangfire / asp.net sample 3 27
Windows Service to Receive TCP Packets 4 27
Image(7) 1 32
Calling stored proc in EDMX 11 17
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now