Converting seconds to hours,minutes and seconds [closed] - c#

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;

Related

How do you convert mins to hour:min in C#? [duplicate]

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');

Get Number from Random String [closed]

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 5 years ago.
Improve this question
How to get the whole number of this random.
Finally, I get a Debug.Log () to display my 757 number
You need to build up the whole number:
Random random = new Random();
int hundred = 100 * random.Next(1, 10);
int ten = 10 * random.Next(0, 10);
int one = random.Next(0, 10);
int result = hundred + ten + one;
Lets assume, the first random gives you 7, the second gives you 5 and the third gives you 7 again. you would end up with:
hundred = 100 * 7 = 700
ten = 10 * 5 = 50
one = 5
result = 700 + 50 + 7 = 757
Then just log result
What you describe is random 3 digit number. So simply:
Random.Range(100, 1000);

slower timer for a clock

i want a timer that doesn't count every second. i want it to run slower
timer = timer + Time.deltaTime;
int itimer = (int)timer;
int minutes = itimer % 60;
int hours = itimer / 60;
string time = hours.ToString() + ":" + minutes.ToString().PadLeft(2, '0');
this is the code now. the clock currently starts at 19:20 and i want the minute counter to go up every 4 seconds or something(i still have to figure the exact timing out). i tried doing "Time.deltaTime*0.9", but the code doesn't work like that. how can i best slow it down with this code? also, when it's 20:00 (or 1200 before conversion) i'd like for something to happen, so i still need access to that number
thank you
Likely the problem you had was a casting error when you tried assigning the result back to timer. This is because by default when you did 0.9 it did it as a double and your variable timer was a float. Add a f to the end of the number to mark it a float and it should work.
timer = timer + Time.deltaTime * 0.9f;
int itimer = (int)timer;
int minutes = itimer % 60;
int hours = itimer / 60;
string time = hours.ToString() + ":" + minutes.ToString().PadLeft(2, '0');

Transform minutes into hours [duplicate]

This question already has answers here:
Allow only numbers to be inserted & transform hours to minutes to calculate gold/min - UPDATED
(2 answers)
Closed 9 years ago.
I've got stuck in my program, i need to calculate the gold/minute but my math formula won't do the desire thing. As I input the hours into a float(something like 1.2 hours) the transformation will be 72 min instead of 80 as I need.
Can you please help me ? I marked in comment below where the problem is.
And here is my code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace YourGold
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to YourGold App! \n------------------------");
Console.WriteLine("Inesrt your gold: ");
int gold = int.Parse(Console.ReadLine());
Console.WriteLine("Your gold is : " + gold);
Console.WriteLine("Inesrt your time(In Hours) played: ");
float hours = float.Parse(Console.ReadLine());
int minutes = 60;
float time = (float)hours * minutes; // Here the calculation are wrong...
Console.WriteLine("Your total time playd is : " + time + " minutes");
float goldMin = gold / time;
Console.WriteLine("Your gold per minute is : " + goldMin);
Console.WriteLine("The application has ended, press any key to end this app. \nThank you for using it.");
Console.ReadLine();
}
}
}
Thanks a lot.
P.S it's related to this question:Allow only numbers to be inserted & transform hours to minutes to calculate gold/min - UPDATED , I update it same as this but i think i should have done a new question as i did now(I'm still learning how to go on with this platform:) )
Use the built-in TimeSpan:
TimeSpan time = TimeSpan.FromHours(1.2);
double minutes = time.TotalMinutes;
TimeSpan.FromHours Method Returns a TimeSpan that represents a specified number of hours, where the specification is accurate to the nearest millisecond.
You can also do:
// string timeAsString = "1:20";
TimeSpan time;
if (TimeSpan.TryParse(timeAsString, CultureInfo.InvariantCulture, out time))
{
double minutes = time.TotalMinutes;
//... continue
}
else
{
// Ask user to input time in correct format
}
Or:
var time = new TimeSpan(0, 1, 20, 0);
double minutes = time.TotalMinutes;
If you really want your program to behave as you want do this.
time = (int)hours * 60 + (hours%1)*100
var minutes = TimeSpan.FromHours(1.2).TotalMinutes; // returns 72.0
var hours = 1.2;
var minutes = ((int)hours) * 60 + (hours%1)*100;
And a side note: such way of inputting time is IMO not a good one. It'll be confusing and I guess that more often than not people will be actually entering 1:20 instead of 1.2, which'll break your application. And if not, they might be entering 1.5 thinking of 90 minutes. I know I would have done it like that.

Retrieving a value that is stored as MB [closed]

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 9 years ago.
Improve this question
I have a table that stores the amount of RAM a server has in a biginit column with values such as 2470208.
But how I can apply a data annotation or other validations to show only 2 instead of s470208. ?
I mean to always divide by 1 million and get the number on the left side of the digit ?
1) Use this for automatic thousands-unit:
string GetByteString(long n) {
int k=0;
string u=" kMGTEP";
while(n>1024) {
n>>=10;
k++;
}
return n.ToString() + u[k];
}
Call:
string s= GetByteString(1234567890123);
Debug.WriteLine(s);
2) But if you simply always want MB just shift by 20:
long n = 123456789;
string MB = (n>>20).ToString();
But this will show 0 if n goes below 1MB.
Reason:
1 kB = 2^10 = 1<<10 = 1024;
1 MB = 2^20 = 1<<20 = 1024*1024 = 1048576;
1 GB = 2^30 = 1<<30 = 1024*1024*1024 = 1073741824;
You tagged C# but mentioned a bigint column so it isn't clear whether you're looking for a database or C# solution. The following C# method will take the number of bytes as an integer and return a formatted string...
public string FormattedBytes(long bytes)
{
string units = " kMGT";
double logBase = Math.Log((double)bytes, 1024.0);
double floorBase = Math.Floor(logBase);
return String.Format("{0:N2}{1}b",
Math.Pow(1024.0, logBase - floorBase),
units.Substring((int)floorBase, 1));
}

Categories

Resources