【问题标题】:Avoid default rounding behaviour with decimals in C#在 C# 中避免使用小数的默认舍入行为
【发布时间】:2013-03-24 06:39:42
【问题描述】:

上下文:我正在使用 Sortable 进行排序。

   $("#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);


                }

在控制器中我有:

    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);
    }

经过几次排序(潜水)后,我得到了一些像 0,0023565 这样的数字,但当我将值发布到控制器(MVC 应用程序)时,它似乎转换为 0。我不想要这个,因为我的代码在经过几次分割后就不起作用了。我不希望我的数字四舍五入。

【问题讨论】:

  • 您的问题似乎出在 jQuery 上,而不是控制器上。你在哪里失去了精确度?
  • 如果我 console.log 结果我得到类似 0.00055 但是当我刷新页面(从数据库接收结果)我得到 0。控制器接收值:0.00055
  • 嗯,那么问题似乎是您在数据库更新中失去了精度。您是否已验证该值是否符合您在数据库中的预期值?
  • 请重述您的问题标题。这是误导。
  • 自发布问题以来我稍微更改了代码,但是当我在更新行上设置断点时,似乎该数字具有必要的精度,但在数据库中只有小数点后的两个数字。数据库中的所有优先级在小数点后都有两个数字。

标签: c# jquery decimal jquery-ui-sortable


【解决方案1】:

您的控制器中似乎无法进行十进制转换。

decimal convertDecimal = Convert.ToDecimal(newpriority.Replace(".", ","),);

您在上面的代码行中将小数点 (.) 替换为逗号 (,)。这是什么原因?

如果无法完成转换,Convert.ToDecimal 方法将默认为 0,这就是 0 的原因。

这是来自 Microsoft 的示例,请更改格式提供程序或使用默认小数位 (.)

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.0123  ->  123456789.0123 
//           123 456 789,0123  ->  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.0123  ->  FormatException 
//           123 456 789,0123  ->  123456789.0123

【讨论】:

  • 看来问题出在精度上。例如,如果我在数据库中得到 3.62255,我只有 3.62
  • 当您调试控制器时,您是否在 convertDecimal 变量中获得了预期的结果?
  • 是的,我得到了预期的结果
  • a.Priority是什么数据类型,能否提供数据库字段的数据类型和精度。
猜你喜欢
  • 1970-01-01
  • 2020-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多