2016-05-22 17 views
0

小数点の値を読み込み、それを使用して決定したいと考えています。値が0.75より大きい場合は、そうでない場合を格納します。私は4/5に分割して答えは0.8ですが、変数は0.8ではなく0を格納しています。C#コードの小数点値の読み込みに問題がある

float value; 

    List<crime> finalList = new List<crime>(); 
    Dictionary<int,float> finalDictionary = new Dictionary<int,float>(); 


    public float matching() 
    { 
     string crimeType = null; 
     string keys = null; 
     List<crime> keywordsFromOldCrimes = new List<crime>(); 
     List<String> keywordsFromCrimes = new List<string>(); 

     List<string> commonWords = new List<string>(); 
     SqlConnection conn = new SqlConnection(@"Data Source=DELL\SQLEXPRESS;initial catalog=CDMIS;integrated security=True;MultipleActiveResultSets=true;"); 
     SqlCommand commd = new SqlCommand("select * from crime where id=0 ", conn); 
     SqlCommand commd1 = new SqlCommand("select * from crime", conn); 
     SqlCommand commd2 = new SqlCommand("select crime.id , crime.crimeType, crime.keywords from crime inner join FIR on crime.FIRid= FIR.id where FIR.incidentDate in (select from FIR where datepart(yy, incidentDate)>2015)", conn); 
     SqlCommand com3 = new SqlCommand("select * from crime where crimedate >= '2015-01-01'",conn); 
     SqlDataReader read1; 
     SqlDataReader read2; 
     try 
     { 
      conn.Open(); 
      read1 = commd.ExecuteReader(); 
      while (read1.Read()) 
      { 
       crimeType = read1.GetString(read1.GetOrdinal("crimeType")); 
       keys = read1.GetString(read1.GetOrdinal("keywords")); 
      } 
     } 
     catch (Exception exp) 
     { 
      throw exp; 
     } 

     read1.Close(); 
     List<string> keywords = keys.Split(',').ToList(); 

     // read2 = commd2.ExecuteReader(); 
     //while (read2.Read()) 
     //{ 
     // keywords.Add(new crime() 
     // { 
     //  id = read2.GetInt32(read2.GetOrdinal("id")), 
     //  keywords = read2.GetString(read2.GetOrdinal("keywords")), 
     //  crimeType = read2.GetString(read2.GetOrdinal("crimeType")) 
     // }); 
     //} 
     //read2.Close(); 
     //foreach (var word in keyword) 
     //{ 
     // if (word.keywords == keys) 
     // { 

     // } 
     //} 
     read1 = com3.ExecuteReader(); 
     while (read1.Read()) 
     { 
      keywordsFromOldCrimes.Add(new crime() 
      { 
       id = read1.GetInt32(read1.GetOrdinal("id")), 
       caseStatus = read1.GetString(read1.GetOrdinal("caseStatus")), 
       crimeType = read1.GetString(read1.GetOrdinal("crimeType")), 
       details = read1.GetString(read1.GetOrdinal("details")), 
       keywords = read1.GetString(read1.GetOrdinal("keywords")), 

      }); 
     } 

     foreach (var crime in keywordsFromOldCrimes) 
     { 
      keywordsFromCrimes = crime.keywords.Split(',').ToList(); 
      commonWords = keywords.Intersect(keywordsFromCrimes).ToList(); 
      value = commonWords.Count()/keywords.Count(); 
      Console.Write(value); 
      if (value > 0.75) 
      { 
       finalDictionary.Add(crime.id, value); 
      } 
      else 
      { 
       Console.Write("nothing common"); 
      } 
     } 

     return (value); 
    } 

} 

問題は、次の部分にあるの下クラスのコードが与えられる。

foreach (var crime in keywordsFromOldCrimes) 
    { 
     keywordsFromCrimes = crime.keywords.Split(',').ToList(); 
     commonWords = keywords.Intersect(keywordsFromCrimes).ToList(); 
     value = commonWords.Count()/keywords.Count(); 
     Console.Write(value); 
     if (value > 0.75) 
     { 
      finalDictionary.Add(crime.id, value); 
     } 
     else 
     { 
      Console.Write("nothing common"); 
     } 
    } 

commonWords.countの私のデータベース値のデータによれば、()4との値を= keywords.count()= 5したがって、変数 "value"の答えは "0.8"でなければなりませんが、 "0"です。

答えて

1

この部分

commonWords.Count()/keywords.Count() 

が整数除算である、それが動作するためにfloat型にキャストすることが適切

commonWords.Count()/(float) keywords.Count() 
+0

感謝を必要とする、それが働いた:) –

関連する問題