쇼핑 사이트에서 일하고 있는데 제품의 부분합을 계산하려고합니다.Jquery 버그? 두 숫자가 곱한 후에 긴 십진수
getJSON 응답 배열에서 배열과 수량에 따른 가격이 있습니다. 그들 중 두 명이
이 소계입니다. 수량을 변경할 수 있으며 다른 부분합이 나옵니다. 나는 특정 번호로 수량을 변경할 때
그러나, 최종 합계는
259.99999999994 또는 일부 긴 진수 같다. console.log를 사용하여 $ price 및 $ qty를 확인합니다. 둘 다 올바른 형식으로되어 있습니다 .299.99 및 6 수량. 나는 어떤 일이 일어나는지 전혀 모른다. 누군가가 그것에 대해 나를 도울 수 있으면 감사하겠습니다.
다음은 내 Jquery 코드입니다.
$(".price").each(function(index, price){
$price=$(this);
//get the product id and the price shown on the page
var id=$price.closest('tr').attr('id');
var indiPrice=$($price).html();
//take off $
indiPrice=indiPrice.substring(1)
//make sure it is number format
var aindiPrice=Number(indiPrice);
//push into the array
productIdPrice[id]=(aindiPrice);
var에 URL = update.php
$.getJSON(
url,
{productId:tableId, //tableId is from the other jquery code which refers to
qty:qty}, productId
function(responseProduct){
$.each(responseProduct, function(productIndex, Qty){
//loop the return data
if(productIdPrice[productIndex]){
//get the price from the previous array we create X Qty
newSub=productIdPrice[productIndex]*Number(Qty);
//productIdPrice[productIndex] are the price like 199.99 or 99.99
// Qty are Quantity like 9 or 10 or 3
sum+=newSub;
newSub.toFixed(2); //try to solve the problem with toFixed but
didn't work
console.log("id: "+productIdPrice[productIndex])
console.log("Qty: "+Qty);
console.log(newSub); **//newSub sometime become XXXX.96999999994**
};
감사를 다시!
남자 ... 나는 내가 가까웠다는 것을 알았다! 고마워요! 이 나쁜 10 진수 계산은 어떻게됩니까? 누구나 아이디어가 있습니까? – FlyingCat
@ Jerry - 수레가 작동하는 방식 일뿐입니다. 정확한 계산이 아닙니다 ... 이것은 부동 소수점 정밀도입니다. 나는 여기에서 농담하지 않고있다, wikipedia는이 화제에 관해보기 흉하지 않은 이해를 얻기위한 최고의 장소 다 : http://en.wikipedia.org/wiki/Floating_point –
ok .. 나는 그것을 조사 할 것이다. .. 다시 한번 감사드립니다. – FlyingCat