2013-04-02 2 views
1

컨텍스트 : Sortable을 사용하여 정렬하고 있습니다. (MVC 응용 프로그램을C에서 십진수로 기본 반올림 동작 피하기

public ActionResult UpdatePicturePriority(int id, string newpriority) 
     { 
     var a = _PictureRepo.GetById(id); 
     decimal convertDecimal = Convert.ToDecimal(newpriority.Replace(".", ","),); 
     a.Priority = convertDecimal; 
     _PictureRepo.Update(a); 
     return Json(true); 
    } 

몇을 분류 할 (divings) 후, 나는 0,0023565 같은 일부 번호를하지만 내가 컨트롤러에 값을 게시 할 때 0로 변환되는 것 같습니다 : 컨트롤러에서

$("#sortable").sortable({ 
     update: function (e,ui) 
     { 
     nextItemPrio=parseFloat(ui.item.next().find("input[name='Priority']").val().replace(",", ".")); 
     prevItemPrio = parseFloat(ui.item.prev().find("input[name='Priority']").val().replace(",", ".")); 
     currentItemPrio = parseFloat(ui.item.find("input[name='Priority']").val().replace(",", ".")); 
        if ((nextItemPrio < currentItemPrio && prevItemPrio < currentItemPrio) 
      || (nextItemPrio > currentItemPrio && prevItemPrio > currentItemPrio)) { 

        ui.item.find("input[name='Priority']").val((prevItemPrio + nextItemPrio)/2.0); 


       } 

내가 가진). 필자는 코드를 몇 번 나누어도 작동하지 않으므로이 작업을 원하지 않습니다. 나는 내 숫자가 반올림되는 것을 원하지 않는다.

+0

문제는 jQuery, 컨트롤러가 아닌 것 같습니다. 어디에서 정밀도를 잃고 있습니까? –

+0

만약 내가 console.log 내가 0.00055 같은 결과를 얻을하지만, 내가 (데이터베이스에서 결과를받을) 페이지를 새로 고칠 때 나는 0. 컨트롤러를받습니다 : 0.00055 –

+0

흠, 그러면 그 문제는 당신이 정확성을 잃어 가고있는 것 같습니다 데이터베이스 업데이트. 그 값이 데이터베이스에서 기대하는 값인지 확인 했습니까? –

답변

1

십진수 변환이 컨트롤러에서 작동하지 않는 것 같습니다.

위의 코드 줄에서 소수점 (.)을 쉼표 (,)로 바꿉니다. 이것에 대한 이유는 무엇입니까?

Convert.ToDecimal 방법은 변환이 따라서 여기에 0

에 대한 이유를 할 수없는 Microsoft의 예 인 경우 디폴트로 0, 중 당신의 형식 공급자를 변경하거나 (기본 소수 자릿수를 사용합니다 .)

using System; 
using System.Globalization; 

public class Example 
{ 
    public static void Main() 
    { 
     string[] values = { "123456789", "12345.6789", "12 345,6789", 
          "123,456.789", "123 456,789", "123,456,789.0123", 
          "123 456 789,0123" }; 
     CultureInfo[] cultures = { new CultureInfo("en-US"), 
           new CultureInfo("fr-FR") }; 

     foreach (CultureInfo culture in cultures) 
     { 
     Console.WriteLine("String -> Decimal Conversion Using the {0} Culture", 
          culture.Name); 
     foreach (string value in values) 
     { 
      Console.Write("{0,20} -> ", value); 
      try { 
       Console.WriteLine(Convert.ToDecimal(value, culture)); 
      } 
      catch (FormatException) { 
       Console.WriteLine("FormatException"); 
      } 
     } 
     Console.WriteLine(); 
     }      
    } 
} 
// The example displays the following output: 
//  String -> Decimal Conversion Using the en-US Culture 
//     123456789 -> 123456789 
//     12345.6789 -> 12345.6789 
//    12 345,6789 -> FormatException 
//    123,456.789 -> 123456.789 
//    123 456,789 -> FormatException 
//   123,456,789.-> 123456789.
//   123 456 789,-> FormatException 
//   
//  String -> Decimal Conversion Using the fr-FR Culture 
//     123456789 -> 123456789 
//     12345.6789 -> FormatException 
//    12 345,6789 -> 12345.6789 
//    123,456.789 -> FormatException 
//    123 456,789 -> 123456.789 
//   123,456,789.-> FormatException 
//   123 456 789,-> 123456789.
+0

문제는 정밀도가 떨어지는 것 같습니다. 예를 들어, 데이터베이스에서 3.62255를 얻은 경우에만 3.62를 갖습니다. –

+0

컨트롤러를 디버깅 할 때 ** convertDecimal ** 변수에서 예상되는 결과를 얻고 있습니까? – RedJandal

+0

예, 예상 결과가 나타납니다 –