aquí es un ejemploCómo convertir números decimales en cuando o viceversa
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 ?
aquí es un ejemploCómo convertir números decimales en cuando o viceversa
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 ?
Cuando la gente habla de horas decimales, por lo general significa 0.1 = 6 minutos.
Por lo tanto, la fórmula correcta para convertir 8,3 sería:
8 horas + 3 * 6 minutos = 08:18
Convertir 08:20 a decimal sería:
8 + 20/6 = 8.333333 (probablemente ronda a 8.3)
Esto no es lo que solicitó OP - especificó 8.20 = 8 horas y 20 minutos. –
Sí, pero es muy posible que la pregunta indique erróneamente el requisito real. No hay un mapeo lógico de horas decimales a minutos que incluya: 20 = .2 y: 30 = .3. Mi respuesta refleja esa probabilidad. –
Esto también no tendría en cuenta 5.11 que daría 5:66 en lugar de 5:06 – yekta
Si siempre se separa con . y quiere que para mostrar a continuación, sólo tiene que utilizar esto:
var ar="8.30".split(new[]{'.'});
Console.Write("{0} hours {1} minutes",ar[0], ar[1]);
PD: Aquí estamos seguros de tener dos elementos en serie, pero por favor, compruebe la longitud de la matriz ar
antes de usar ar[1]
He aquí un par de métodos de extensión (por DateTime y decimal) que hacen el trabajo:
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;
}
}
probé esto muy rápidamente en una página web ASP.net (que tenía un proyecto web abierta en ese momento) utilizando la siguiente en una nueva página en blanco Y parecía que funcionaba una delicia:
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());
}
esta solución es para mí .. –
Según Rob pero sustituya
string[] parts = value.ToString().Split(new char[] { '.' });
int hours = Convert.ToInt32(parts[0]);
int minutes = Convert.ToInt32(parts[1]);
como
int hours = (int)value;
int minutes = (int)((value - minutes) * 100);
sin condiciones o dependencia de la cultura actual (el supuesto de que el'. 'es el punto decimal)
Mi enfoque sería algo como esto. (Este es el rubí por lo que tendrá que convertirlo a sí mismo, pero la lógica es cuál es importante aquí)
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
Dónde findTime ("5,015") le da el valor de tiempo apropiado.
He probado esto en las siguientes pruebas y todas pasan.
| 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" |
¿Cómo puedo analizar el txtDuration.Text
valor en un valor decimal?
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);
}
¿Qué será 8.90 según usted? – rahul
tipo de similar a http://stackoverflow.com/questions/1345504/method-to-subtract-or-add-time – RaYell
¿es esta una pregunta de tarea? –