小数点无法通过 JSON 回发正确传递到 C#

发布于 2024-12-14 19:56:55 字数 145 浏览 4 评论 0原文

我使用 JSON 和 jQuery AJAX 调用从前端传回产品价格。每当我传回一个整数(21,而不是 21.50)的小数时,JSON 对象不包含小数点,并且不会在生成的操作方法上填充我的小数属性。有没有一种简单的方法可以强制 jscript 始终显式包含 .00 作为整数?

I'm passing a product price back from my front end using JSON and the jQuery AJAX call. Whenever I pass a decimal back that is just an integer (21, instead of 21.50) the JSON object doesn't include the decimal point and it isn't filling in my decimal property on the resulting action method. Is there a simple way to force the jscript to always explicitly include the .00 for round numbers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仄言 2024-12-21 19:56:55

如果您想强制它具有小数,只需在构建回调数据时对数字调用 toFixed 函数即可。

var flatNum = 21;

$.ajax({
  data: {
    name: "Name",
    num: flatNum.toFixed(2);
  },
  ...
});

这样做会将数字填充到小数点后两位(如果精度长于此,则四舍五入)。

If you want to force it to have a decimal, just call the toFixed function on the number when you're building your data for the callback.

var flatNum = 21;

$.ajax({
  data: {
    name: "Name",
    num: flatNum.toFixed(2);
  },
  ...
});

Doing that will pad the number out to two decimal places (and round if the precision is longer than that).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文