我需要计算几个数字。它到目前为止工作正常,但它失去原来的小数位(这里:50美分)。知道为什么会这样吗?
$("select").on("change", function () {
var sum1 = parseFloat($(this).val()) * parseFloat($(this).data("price"));
var sum2 = parseFloat($("#orgPrice").data("price"));
var sum3 = parseFloat(sum1) + parseFloat(sum2);
$("#total").text(sum3.toFixed(2));
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="orgPrice" data-price="100,00"></div>
<select id="subcode_060" class="custom-select custom-select-sm" data-description="UPGRADES" data-subdescription="X to Y" data-price="3560,50">
<option value="0" selected>0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<div id="total">0.00</div>
&#13;
答案 0 :(得分:3)
您必须使用.
而不是,
查看此处发生的唯一更改,来自
data-price="3560,50"
到
data-price="3560.50"
$("select").on("change", function () {
var sum1 = parseFloat($(this).val()) * parseFloat($(this).data("price"));
var sum2 = parseFloat($("#orgPrice").data("price"));
var sum3 = parseFloat(sum1) + parseFloat(sum2);
$("#total").text(sum3.toFixed(2));
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="orgPrice" data-price="100,00"></div>
<select id="subcode_060" class="custom-select custom-select-sm" data-description="UPGRADES" data-subdescription="X to Y" data-price="3560.50">
<option value="0" selected>0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<div id="total">0.00</div>
&#13;
注意:当然,您还希望将data-price="100,00"
更改为data-price="100.00"
它只是没有真正影响它的.00
public class MyClass
{
public int Id { get; set; }
public int? ParentId { get; set; }
public string Title { get; set; }
public DateTime Created { get; set; }
public string Description { get; set; }
public IEnuerable<MyClass> Children { get; set; }
}
}
答案 1 :(得分:1)