Convert decimal (2.75) to time (2:45) [duplicate] - c#

This question already has answers here:
C# String Format for hours and minutes from decimal
(3 answers)
Closed 9 years ago.
If I have a double like 2.75, is there a way in .Net to format it as '2:45'
If it is for example, 2.75555555555, it should round it to the nearest minute.
I would not mind coding this myself, but I am wondering if .Net can. I checked ToString but did not find anything.
Thanks

Use TimeSpan and its ToString formatter:
TimeSpan timespan = TimeSpan.FromHours(2.75);
string output = timespan.ToString("h\\:mm");
For example
TimeSpan.FromHours(2.75555).ToString("h\\:mm")
outputs
2:45

Related

Correct way to precisely round decimals in C# [duplicate]

This question already has answers here:
How do I make it round off instead of always round down?
(2 answers)
How to round a decimal up?
(6 answers)
Closed 5 months ago.
This is my code:
decimal test1 = 190.5m;
decimal test2 = 191.5m;
var testResult1 = Math.Round(test1, 0); // =190 It should be 191!!!!
var testResult2 = Math.Round(test2, 0); // =192 Correct.
Is there a way I could get the correct result? Maybe another library instead of System.Math?
This is by design. Albeit not what we have grown up with.
Find how you manage to round the way you expect here:
https://learn.microsoft.com/en-us/dotnet/api/system.midpointrounding?redirectedfrom=MSDN&view=net-6.0
For a bit more reading and background, have a browse through some of the answers here:
Why does .NET use banker's rounding as default?

Convert 24-hour string value to timespan [duplicate]

This question already has answers here:
Parse exact custom time format?
(2 answers)
Closed 5 years ago.
I'm reading data from an excel file. The user can specify the time in one of the following formats:
1600
16:00
In the latter case, I can easily convert it to a timespan. How do I convert the first format to timespan? Is there a straightforward way than manipulating the string to convert to the latter and do it?
Use
TimeSpan span = TimeSpan.ParseExact("1600", new string[] {"hhmm", #"hh\:mm"}, CultureInfo.InvariantCulture);

Round Timespan Seconds [duplicate]

This question already has answers here:
Can you round a .NET TimeSpan object?
(10 answers)
Closed 6 years ago.
Round timespan seconds I also need do same with .Hours and .Minutes
This:
1.53994 second
to:
1 second
This code worked for me.
TimeSpan ts = TimeSpan.FromSeconds(1.54);
Math.Round(ts.TotalSeconds);

Convert String to Date only in C# [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert String to Date in .NET
I new here, I'm stuck with this problem when a have an array of strings with values of "120612" and "200612".
I need to convert the strings to a date format like this, "Tues 12 June, 2012". How will I do that?
Thanks in advance.
DateTime.ParseExact is probably what you're looking for:
DateTime.ParseExact("200612", "ddMMyy", System.Globalization.CultureInfo.CurrentCulture)

String conversion C# [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert from scientific notation string to float in C#
Is it there an build-in function which converts string in format "2.71e+006" to a number or I have to write my custom algorithm?
The Decimal Parse method has an overload you can use:
decimal d = Decimal.Parse("2.71e+006", System.Globalization.NumberStyles.Float);
You can also do the same for a Double.

Categories