0

I have a field in db named 'loss' which is of type float.

Here is how I save it in db:

[HttpPost]
public int SaveLoss(float loss)
{
   var t = (from x in db.tblTest
            where x.id == 8
            select x).First();
   t.loss = loss; //also tried t.loss = loss / 1;
   db.SaveChanges(); //entity framework
 }

This is how I always work and in all the other tables it works fine.

I enter the loss via a textbox and here is the Ajax call:

$.ajax({
    type: "POST",
    url: "/Test/SaveLoss",
    data: {
        loss: parseFloat($('#myTextbox').val().replace(',', '.'))
    }
    ....

If in the textbox I enter 66.55, everything works great. If in the textbox I enter 66,55, the value stored in db is 6655. This issue doesn't happen on my computer. When I publish the application on server, it occurs. Why is this annoying issue happening and how to fix it?

Fiddler says that 66.55 is input parameter of SaveLoss.

petko_stankoski
  • 10,459
  • 41
  • 127
  • 231

3 Answers3

4

It looks like either the browser or server has a different number format, perhaps it is running in a culture where ',' is used as a decimal separator and '.' as a thousands separator.

You could try setting Thread.CurrentThread.CurrentCulture to CultureInfo.InvariantCulture after starting the server or before running the database query.

g t
  • 7,287
  • 7
  • 50
  • 85
  • +1 Was thinking along the same lines http://stackoverflow.com/questions/793459/how-to-set-decimal-separators-in-asp-net-mvc-controllers – mlorbetske Jan 10 '13 at 08:29
0

If You have generated EF mapping using database first. You should be concerned about EntityFramework..

EF maps SQL column type float into C# type double not float

see: SqlClient for Entity FrameworkTypes

aifarfa
  • 3,939
  • 2
  • 23
  • 35
  • In that case, `Loss` (from the data object) would be a `double`, when it's assigned `loss` from the controller (`float`) it'd be converted to a `double` for the assignment. – mlorbetske Jan 10 '13 at 08:27
0

Pass it as string and try like this while saving in entity framework:

var amount = decimal.Parse(InvoiceAmount, NumberStyles.Any);
Nalan Madheswaran
  • 10,136
  • 1
  • 57
  • 42