Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# MVC 4 model changing floating point?

I have some problems with globalization and floating points. Intresting thing is if i send data in en-GB and floating point is "." (dot) all seems looks fine

floating point in post and model is thesame

but when i change culture with coma "," then all going weird :)

enter image description here

Now in screen above u can see POST sending "0,07" but model give me "7". Any ideas how to fix this problem ?

Tx for answers and ideas :)

like image 534
Andrzej Siwek Avatar asked Oct 20 '22 20:10

Andrzej Siwek


1 Answers

Be sure to set CurrentThread.CurrentCulture because that is the one used for parsing the number.

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_AcquireRequestState(object sender, EventArgs e)
    {
        var culture = CultureInfo.CreateSpecificCulture("pl-PL");
        Thread.CurrentThread.CurrentUICulture = culture;
        Thread.CurrentThread.CurrentCulture = culture;
    }
}
like image 114
LostInComputer Avatar answered Nov 17 '22 16:11

LostInComputer