1

Context: I'm doing sorting using 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);


                }

In controller I have:

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

After few sortings (divings),I get some number like 0,0023565 but it seems that is convert to 0 when I post the value to controller (MVC application). I don't want this, because my code doesn't work after few dividings. I don't want that my numbers get rounded.

Vlado Pandžić
  • 4,879
  • 8
  • 44
  • 75
  • It seems like your issue is in the jQuery, not the controller. Where are you losing precision? – David L Apr 02 '13 at 19:56
  • If I console.log the results I get something like 0.00055 but when I refresh page (receive results from database) I get 0. Controller receives value: 0.00055 – Vlado Pandžić Apr 02 '13 at 20:01
  • Hmm, would seem then that the issue is that you're losing precision in your database update. Have you verified that the value is what you expect in your database? – David L Apr 02 '13 at 20:32
  • Please restate your question title. It is misleading. – Dan Esparza Apr 02 '13 at 20:37
  • I changed a code a little bit since posting question but when I put breakpoint on updating line , it seems the number has necessery precision, but in database has only two numbers after decimal point. All priorities in database have two numbers after decimal point. – Vlado Pandžić Apr 02 '13 at 20:40

1 Answers1

1

It looks like you decimal conversion is not working in your controller.

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

You are replacing the decimal point (.) with a comma (,) in the line of code above. What is the reason for this?

The Convert.ToDecimal method will default to 0 if the conversion can not be done hence the reason for the 0.

here is the example from Microsoft, either change your format provider or use the default decimal place (.)

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
RedJandal
  • 1,203
  • 10
  • 18