2009-08-28 5 views
6

ここでは一例でありまたはその逆

  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 ?  
+4

あなたは8.90になりますか? – rahul

+1

http://stackoverflow.com/questions/1345504/method-to-subtract-or-add-time – RaYell

+1

と似たような種類の宿題ですか? –

答えて

11

人は小数点時間について話すとき、彼らは通常0.1 = 6分を意味します。

したがって、8.3を変換する正しい式は次のようになります。

8時間+ 3 * 6分= 8:18

10進数に8時20分に変換するためには、次のようになります

8 + 20/6 = 8.333333(おそらく8.3への丸め)

+0

これはOPが要求したものではなく、8.20 = 8時間20分と指定されています。 –

+7

はい、質問には実際の要件が誤っている可能性があります。 20 = .2と:30 = .3を含む10進数時間から分までの論理マッピングはありません。私の答えはその可能性を反映しています。 –

+0

これはまた、5.11ではなく5.66を与える5.11を考慮しないでしょう。 – yekta

3

いつもで区切られている場合。とは、単にこれを使用、次に表示するためにそれをしたい:

var ar="8.30".split(new[]{'.'}); 

Console.Write("{0} hours {1} minutes",ar[0], ar[1]); 

PS:ここでは、配列内の2つの要素を持っているのは確実だが、ar[1]

1

を使用する前に、アレイarの長さを確認してくださいここのカップルです仕事をする(日時と小数点のための)拡張メソッド:

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; 
    } 
} 
私は新しい空白のページに次のように使用して(私は一度に開いてWebプロジェクトを持っていた)のASP.net Webページに非常に迅速にこれをテストし

、およびそれ御馳走を動作するように見えません:

ロブあたりとして
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()); 
} 
+0

この解決策は私のために働いています.. –

0

しかし

int hours = (int)value; 
int minutes = (int)((value - minutes) * 100); 

として代替

string[] parts = value.ToString().Split(new char[] { '.' }); 
int hours = Convert.ToInt32(parts[0]); 
int minutes = Convert.ToInt32(parts[1]); 

何の文字列や、現在の文化への依存「(仮定。 'は小数点です)

2

私のアプローチは次のようになります。 findTimeは(「5.015」)は、あなたに適切な時間値を与えるどこ

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 

(これは、あなたがそれを自分で変換する必要がありますのでルビーですが、ロジックがここにいただきましたが重要です)。

私はこれを次のテストでテストしましたが、すべてが合格しています。

 | 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"    | 
0

txtDuration.Textの値を10進数で解析するにはどうすればよいですか?

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); 
} 
関連する問題