tutaj jest przykłademJak przekonwertować liczbę dziesiętną do czasu lub odwrotnie
if 8.30 is there it should be 8 hours 30 minute
if 8 hour 20 minutes then 8.20
Please tell whether it is possible ? if yes
how ?
tutaj jest przykłademJak przekonwertować liczbę dziesiętną do czasu lub odwrotnie
if 8.30 is there it should be 8 hours 30 minute
if 8 hour 20 minutes then 8.20
Please tell whether it is possible ? if yes
how ?
Kiedy ludzie mówią o godzinach po przecinku, to zwykle znaczy 0,1 = 6 minut.
więc, prawidłowy wzór konwersji 8.3 to:
8 godzin + 3 * 6 minut = 08:18
celu przekształcenia 8:20 dziesiętnej byłoby:
8 + 20/6 = 8,333333 (prawdopodobnie zaokrąglone do 8,3)
Nie o to prosił OP - wyszczególnił 8.20 = 8 godzin i 20 minut. –
Tak, ale pytanie prawdopodobnie błędnie określa faktyczne wymagania. Nie ma logicznego odwzorowania od dziesiętnych godzin do minut, które obejmują: 20 = .2 i: 30 = .3. Moja odpowiedź odzwierciedla to prawdopodobieństwo. –
To również nie stanowi 5.11, co dałoby 5:66 zamiast 5:06 – yekta
Jeśli zawsze będzie oddzielony od . i chcesz go do wyświetlania po prostu użyć tego:
var ar="8.30".split(new[]{'.'});
Console.Write("{0} hours {1} minutes",ar[0], ar[1]);
PS: Tu na pewno mają dwa elementy w tablicy, ale proszę sprawdzić długość tablicy ar
przed użyciem ar[1]
Oto kilka rozszerzenie metod (na DateTime i po przecinku), które wykonują zadania:
public static class DecimalToTimeConverters
{
public static DateTime ToDateTime(this decimal value)
{
string[] parts = value.ToString().Split(new char[] { '.' });
int hours = Convert.ToInt32(parts[0]);
int minutes = Convert.ToInt32(parts[1]);
if ((hours > 23) || (hours < 0))
{
throw new ArgumentOutOfRangeException("value", "decimal value must be no greater than 23.59 and no less than 0");
}
if ((minutes > 59) || (minutes < 0))
{
throw new ArgumentOutOfRangeException("value", "decimal value must be no greater than 23.59 and no less than 0");
}
DateTime d = new DateTime(1, 1, 1, hours, minutes, 0);
return d;
}
public static Decimal ToDecimal(this DateTime datetime)
{
Decimal d = new decimal();
d = datetime.Hour;
d = d + Convert.ToDecimal((datetime.Minute * 0.01));
return d;
}
}
Przetestowałem to bardzo szybko w sieci web ASP.NET (miałem projekt internetowy otwarty w tym czasie) przy użyciu następujących w nowej pustej strony I wydawało się pracować leczeniu:
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Decimal d = new decimal();
d = 3.45M;
Response.Write(d.ToDateTime().ToString());
Response.Write("<br />");
DateTime d2 = new DateTime(2009, 1, 1, 4, 55, 0);
Response.Write(d2.ToDecimal().ToString());
}
to rozwiązanie działa dla mnie .. –
Według Roba ale zastąpić
string[] parts = value.ToString().Split(new char[] { '.' });
int hours = Convert.ToInt32(parts[0]);
int minutes = Convert.ToInt32(parts[1]);
jak
int hours = (int)value;
int minutes = (int)((value - minutes) * 100);
No Strings lub polegania na aktualnej kulturze (przy założeniu, że”. "jest kropką dziesiętną)
Moje podejście wyglądałoby mniej więcej tak. (To jest rubin więc trzeba konwertować siebie, ale logika jest co jest ważne tutaj)
def zeropad(number)
return ((number.to_f < 10) ? "0" : "") + number.round.to_s
end
def decimal_to_time(value)
t = value.split(".") #returns an array of ["hour", "minutes"]
hours, minutes = t[0], t[1]
minutes = zeropad((minutes.to_f/10**minutes.length) * 60) # parse the minutes into a time value
return (minutes.to_i == 0) ? hours : hours + ":" + minutes
end
def findTime(value)
value =~ /^\d+\.\d+/ ? decimal_to_time(value) : value
end
Gdzie findTime („5,015”) daje odpowiednią wartość czasową.
Testowałem to przez następujące testy i wszystkie one przeszły.
| entered_time | expected_results|
| "5.6" | "5:36" |
| "5.9" | "5:54" |
| "5.09" | "5:05" |
| "5.0" | "5" |
| "5.00" | "5" |
| "5.015" | "5:01" |
| "6.03" | "6:02" |
| "5.30" | "5:18" |
| "4.2" | "4:12" |
| "8.3" | "8:18" |
| "8.33" | "8:20" |
| "105.5" | "105:30" |
| "16.7" | "16:42" |
| "Abc" | "Abc" |
| "5:36" | "5:36" |
| "5:44" | "5:44" |
Jak mogę zanalizować wartość txtDuration.Text
na wartość dziesiętną?
if (txtDuration.Text)
{
var duration = int.Parse(txtDuration.Text);
var timespan = Boolean.Parse(hdfToggleDuration.Value) ? new TimeSpan (0, 0, duration, 0) : new TimeSpan (0, duration, 0, 0);
DateTime end = start.Add(timespan);
}
Co będzie według Ciebie? – rahul
rodzaj podobnego do http://stackoverflow.com/questions/1345504/method-to-subtract-or-add-time – RaYell
czy to jest zadanie domowe? –