gpt4 book ai didi

javascript - 小于条件不适用于 float

转载 作者:行者123 更新时间:2023-11-28 13:58:45 28 4
gpt4 key购买 nike

这是我的 jquery/javascript 代码:

amount_sale = parseFloat($('#p_sale span').html()).toFixed(2);
amount_cash = parseFloat($('#p_cash span').html()).toFixed(2);

if (amount_cash < amount_sale)
{
alert('Cash amount must be greater than or equal to sale amount');
return;
}

假设 html 如下:

<p id="p_sale">Sale: <span>10.00</span></p>
<p id="p_cash">Cash: <span>20.00</span></p>

无论出于何种原因,即使 p_cash 范围内的内容大于 p_sale 范围内的内容,我仍然会收到警报。

我不明白。

最佳答案

toFixed 将数字转换为固定字符串。在此之前,您只有一个数字,没有精度。我确定你想要:

amount_sale = parseFloat($('#p_sale span').html())<s>.toFixed(2)</s>;
amount_cash = parseFloat($('#p_cash span').html())<s>.toFixed(2)</s>;

if (amount_cash < amount_sale)
{
alert('Cash amount must be greater than or equal to sale amount');
return;
}

通常情况下,这仍然适用于 10.0020.00 等值 - 但绝对不适用于 128.503.14 >.

关于javascript - 小于条件不适用于 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6604461/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com