I have a function that uses fix and rem functions but if the entered number is big it does not give the right number.
    조회 수: 5 (최근 30일)
  
       이전 댓글 표시
    
double d; 
function denominationsCounter(d)
double d50; 
double d20; 
double d10;
double d5; 
double d2;
double d1; 
double d50p; 
double d20p; 
double d10;
double d5p; 
double d2p; 
double d1p; 
d50 = fix(d/50); 
d = rem(d,50); 
d20 = fix(d/20); 
d = rem(d,20); 
d10 = fix(d/10); 
d = rem(d,10); 
d5 = fix(d/5); 
d = rem(d,5); 
d2 = fix(d/2); 
d = rem(d,2); 
d1 = fix(d/1); 
d = rem(d,1); 
d50p = fix(d/0.5); 
d = rem(d,0.5); 
d20p = fix(d/0.2); 
d = rem(d,0.2); 
d10p = fix(d/0.1); 
d = rem(d,0.1); 
d5p = fix(d/0.05); 
d = rem(d,0.05); 
d2p = fix(d/0.02); 
d = rem(d,0.02); 
d1p = fix(d/0.01); 
d = rem(d,0.01); 
disp("Number of Notes: 50:"+d50 +",20:"+ d20 +",10:"+d10+",5:"+d5)
disp("Number of Coins: 2:" +d2 + ",1:"+ d1 + ",50p:" +d50p + ",20p:" +d20p + ",10p:" +d10p + ",5p:" + d5p + ",2p:" +d2p+ ",1p:"+d1p)
disp(d)
This function will calculate the least number of denominations required for an entered amount
for example if I entered 9.33, it show this 10p:0,5p:0,2p:1,1p:1 which means that it will take 1 of 2p coins and 1 of 1p coins for the required amount. while if I entered 15.33 or 20.33 it will show 2p:1,1p:0. It will not add the 1p coin. If the reminder is 1 it is not showing 1p:1 it is showing as zero. Any ideas please to make it suitable for all numbers? 
댓글 수: 1
  Walter Roberson
      
      
 2021년 12월 12일
				Note: MATLAB does not use declarations of variables like that.
double d50;
would be interpreted as
double('d50');
which would look up the codes used to encode 'd' '5' '0' and convert those uint16 to double precision, and then throw away the result because of the semi-colon.
채택된 답변
  Walter Roberson
      
      
 2021년 12월 12일
        function denominationsCounter(D)
F = 100;
d = round(D * F);
f = round(50*F);
d50 = fix(d/f); 
d = rem(d,f); 
f = round(20*F);
d20 = fix(d/f); 
d = rem(d,f); 
f = round(10*F);
d10 = fix(d/f); 
d = rem(d,f); 
f = round(5*F);
d5 = fix(d/f); 
d = rem(d,f); 
f = round(2*F);
d2 = fix(d/f); 
d = rem(d,f); 
f = round(1*F);
d1 = fix(d/f); 
d = rem(d,f); 
f = round(0.5*F);
d50p = fix(d/f); 
d = rem(d,f); 
f = round(0.2*F);
d20p = fix(d/f); 
d = rem(d,f); 
f = round(0.1*F);
d10p = fix(d/f); 
d = rem(d,f); 
f = round(0.05*F);
d5p = fix(d/f); 
d = rem(d,f); 
f = round(0.02*F);
d2p = fix(d/f); 
d = rem(d,f); 
f = round(0.01*F);
d1p = fix(d/f); 
d = rem(d,f); 
disp("Number of Notes: 50:"+d50 +",20:"+ d20 +",10:"+d10+",5:"+d5)
disp("Number of Coins: 2:" +d2 + ",1:"+ d1 + ",50p:" +d50p + ",20p:" +d20p + ",10p:" +d10p + ",5p:" + d5p + ",2p:" +d2p+ ",1p:"+d1p)
disp(d/F)
추가 답변 (1개)
  Voss
      
      
 2021년 12월 12일
        In this case, since you know (presumably) that any number input into the program will be a multiple of 0.01 (i.e., no fractional cents/pence allowed), a solution is to round d to 2 decimal places each time it's calculated, e.g.:
d = round(rem(d,50),2); % and so on every time d is updated
Aternatively, you can redefine d to be in units of cents/pence rather than dollars/pounds/euros/whatever and then just round to the nearest integer.
댓글 수: 3
참고 항목
카테고리
				Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!