简体   繁体   中英

How do you deal with decimal (,) and javascript?

I've an ASP.NET MVC application, C# , and some numbers into my Model in decimal type, which treat the fractional part as comma (ie 12,5).

I serialize them and send to the client using JSON , which correctly convert the comma to point:

var result = Json(new { Value = myModel.myValue }); // become Value = 12.5

Than I process the data client side, with some math function, getting the number value always with point (ie 12.5 * 3 = 37.5).

But, when I need to post back to the server the processed value, if I keep the point and I store the value into my Model (which is decimal , as said), it truncate the values after the point.

Do I really need to do result.replace('.', ',') before sending back data client side? Damn not so good. Best practices?

The paradox is that for mvc's jquery validator (being decimal required) I need to print the value into the input box with comma. The round-trip is crazy...

您可以使用stringify将JavaScript对象转换为字符串,

var myJSON = JSON.stringify(Yourvalue);

This may be helpful, rather than burdening the server with the client's wild choice of encoding, get the javascript to do it.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

It nicely turns 98.76 into 98,76 when passing it through 'it-IT'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM