Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
MyTime calculation
//Calculate time value //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { MyTime timeIn = new MyTime(1f); timeIn = new MyTime("-0915"); Console.WriteLine(timeIn.ToDouble()); Console.WriteLine(timeIn.ConvertToHour(-300.25f)); Console.WriteLine(timeIn.ConvertToDecimal("3015")); } } public class MyTime { double timeValue = 0f; public MyTime() : this(0f) { } public MyTime(double timeValue) { this.timeValue = timeValue; } public MyTime(string value) { this.timeValue = this.ConvertToDecimal(value); } public double ToDouble() { return this.timeValue; } public double ConvertToDecimal(string value) { string hour = "00"; string minute = "00"; Match match = Regex.Match(value, @"^-?(\d{2,}).?(\d{2})$"); if (match.Success) { hour = match.Groups[1].Value; minute = match.Groups[2].Value; } double result = Convert.ToDouble(hour) + (Convert.ToDouble(minute) / 60f); if (value.StartsWith("-")) /* negative value */ { result *= -1f; } return result; } public string ConvertToHour(double value) { int hour = Convert.ToInt32(value); double minute = Math.Abs(value) - Math.Abs(hour); return string.Format("{0:00}{1:00}", hour, (minute * 60f)); } } }
run
|
edit
|
history
|
help
0
Fórum ➡ Convert from one format into another using LINQ ( as much as possible ) ♦
zmeika
ShortBubbleSort
nn
Random number + IF var change2
asxasxd
Games
Enumeration
Test equality...1
hivix