This question already has answers here:
How can I convert seconds into (Hour:Minutes:Seconds:Milliseconds) time?
(14 answers)
Convert minutes to full time C#
(7 answers)
Closed 1 year ago.
So I am getting output in #s and I need to convert this to hour:min form.
For example, 50 mins = 00:50 and 100 mins would be 01:40.
How do I accomplish this?
lbltime.Text = ??.ToString();
For Example
if you means the 50 mins is a parameter int mins = 50;
then:
int mins = 50;
lbltime.Text = (mins / 60).ToString().PadLeft(2,'0') + ":" + (mins - ((int)(mins / 60)) * 60).ToString().PadLeft(2, '0');
Related
This question already has answers here:
How do I round down a decimal to two decimal places in .NET?
(5 answers)
Closed 5 months ago.
I have the number 12.1799999 and it should become 12.17
How does it work in c#?
var n = 12.1799999M;
n = Math.Floor(n * 100) / 100;
If you intent to round down, then use Math.Floor(number, amountOfDecimals)
amountOfDecimals should be 2 in your case
This question already has answers here:
1/252 = 0 in c#?
(3 answers)
Closed last year.
double celsius_0 = (100.5 - 32) * (5 / 9);
double celsius_1 = (100.5- 32) * 5 / 9;
Console.WriteLine(celsius_0);
Console.WriteLine(celsius_1);
output:
0
38,0555555555556
Why do they return differnt values?
I dont understand whats happening
It's because here celsius_0 = (100.5 - 32) * (5 / 9)
you count value in the first bracket then in the second one and then you multiply them.
While in the second line (100.5- 32) * 5 / 9 you count value in bracket then multiply it by 5 and at the end divide it by 9
To sum up in mathematics first you do things in brackets and then multiply/divide and finally add/substract
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I've been trying to make a web app that converts seconds to hours/minutes/seconds but with no luck. I can't figure out how to do this at all, i created over a dozen c# projects but none works with any code i try and i also do not understand the creation of such thing. I would greatly appreciate it if someone could help me create this form.
int seconds = 1000;
TimeSpan timespan = TimeSpan.FromSeconds(seconds);
int hour = timespan.Hours;
int min = timespan.Minutes;
int sec = timespan.Seconds;
You can use simple aritmetic, for example:
int hours;
int minutes;
int seconds;
seconds = 12351;
hours = (int)(Math.Floor((double)(seconds / 3600)));
seconds = seconds % 3600;
minutes = (int)(Math.Floor((double)(seconds / 60)));
seconds = seconds % 60;
string time = hours + ":" + minutes + ":" + seconds;
If there should be a leading zero on the hours, the final line can also be changed to
string time = (hours.ToString().Length == 1 ? "0" + hours : hours) + ":" + minutes + ":" + seconds;
var timeSpan = TimeSpan.FromSeconds(500000);
var totalMinutesInTimeSpan = timeSpan.TotalMinutes;
This question already has answers here:
How to round a integer to the close hundred?
(10 answers)
Closed 7 years ago.
How can i round an (int) so that a number like (22536) is equal to 22000 or 23000?
I haven't found a specific method in the Math class, Math.Round seems to round double only to the nearest int.
By using modulus:
int x = 1500;
int result = x % 1000 >= 500 ? x + 1000 - x % 1000 : x - x % 1000;
It checks if x has any more than 499 when the thousands are stripped, and then rounds it.
This question already has answers here:
What is the best way to divide two TimeSpan objects?
(4 answers)
Closed 5 years ago.
I have a value in TimeSpan, let's say: tsp1 = 2 hour 5 minutes.
I have another TimeSpan variable which contains a value like: tsp2 = 0 hours 2 minutes
Please tell me how I can divide tsp1 by tsp2 so that I can get the exact number of times tsp2 divides into tsp1 and what the remainder is.
I am using Visual Studio 2008.
Thanks.
The simplest approach is probably just to take their lengths in ticks, and divide those. For example:
long ticks1 = tsp1.Ticks;
long ticks2 = tsp2.Ticks;
long remainder;
long count = Math.DivRem(ticks1, ticks2, out remainder);
TimeSpan remainderSpan = TimeSpan.FromTicks(remainder);
Console.WriteLine("tsp1/tsp2 = {0}, remainder {1}", count, remainderSpan);
a div b:
double adivb = (double)a.Ticks/b.Ticks;
edited:
i found another post on th same topic
How can I achieve a modulus operation with System.TimeSpan values, without looping?
An int will hold enough seconds for ~64 years, so as long as you stay well below that:
int count = (int) (tsp1.t.TotalSeconds / tsp2.t.TotalSeconds);
double remainder = tsp1.t.TotalSeconds - (count * tsp2.t.TotalSeconds);
And maybe convert the remainder to int as well.