현재 2 개의 double과 1 개의 세금이 제외 된 가격 및 세금 비율이 있습니다.objective-c에 곱하면 예상치 못한 동작이 발생합니다.
가격 : 9.79 (bascially 11 만 세금 별도) 세금 : 11 %
이제 무엇을해야하는지 다시 가격 세금 포함를 얻는 것입니다, 그래서이 수행
// Tax exclusive test
double taxRate = 0.11;
double priceWithTax = 11;
double priceWithoutTax = priceWithTax * (1.0 - taxRate);
NSLog(@"priceWithoutTax = %f", priceWithoutTax);
double result = priceWithoutTax * (1 + taxRate);
NSLog(@"Result: %f", result);
을 하지만 실행 결과는 다음과 같습니다.
priceWithoutTax = 9.790000
Result: 10.866900
예상 출력은 다시 11입니다. 누구든지 멋진 일을 도울 수 있다면 float 정밀도에 관한 많은 소스를 확인했지만 대답을 찾을 수없는 것 같습니다.
미리 감사드립니다.
UPDATE (밧세바 의해 답변을 한 후 새로운 코드)
// Tax exclusive test
double taxRate = 0.11;
double priceWithTax = 11;
double priceWithoutTax = priceWithTax * (1.0 - taxRate);
NSLog(@"priceWithoutTax = %f", priceWithoutTax);
double result = priceWithoutTax/(1 - taxRate);
NSLog(@"Result: %f", result);
결과 :
priceWithoutTax = 9.790000
Result: 11.000000
지금 시도해보십시오. 시도 할 때 질문이 업데이트됩니다. –
작동합니다! !!!! 감사!! 당신은 오늘 확실히 영웅입니다! –
나는 당신의 대답을 받아 들일 것이지만 나는 8 분 후에 만 ... –