I have this code (@Model.TW is 11,58 and @Model.TK is 27,65)(both doubles):
var parameters = [@Model.TW,@Model.TK];
When checking parameters[0] and [1] the double values are rounded to 11 and 27. Why is this?
The same happens when just doing
var double = [@Model.TW];
or
var double = parseFloat('[@Model.TW]');
even if the return types of @Model.xx are changed to string there is no difference.
Float values passed to JavaScript as JSON or direct values must have "." as decimal separator. Some cultures use "," and thus parsing of such numbers as JSON or with parseFloat
will fail to recognize decimal part.
Fix: use invariant culture to format numbers or use existing libraries (like Json.Net) to produce JSON.
var parameters = [@(Model.TW.ToString(CultureInfo.InvariantCulture)),
@(Model.TK.ToString(CultureInfo.InvariantCulture))];
JSON sample can be found at - How do I write unencoded Json to my View using Razor?