How can I round to 2 decimal places

조회 수: 7 (최근 30일)
fernando piedra
fernando piedra 2020년 8월 27일
답변: Walter Roberson 2020년 8월 27일
So I am trying to round my number my current output is giving me 4 decimal places after the period 127.1234 im trying to make look like this 127.12. i have tried all formats of rounding and have multiple by 100 and divide by 100 nothing really seems to work.
oad a02_data.mat
sub_tot = sum(purchases(:,1));
sub_tot_0 = 0;
sub_tot_1 = 0;
sub_tot_2 = 0;
tax_tot_0 = 0;
tax_tot_1 = 0;
tax_tot_2 = 0;
L = length(purchases(:,1));
for ind = 1:L
if purchases(ind,2) == 0
sub_tot_0 = sub_tot_0 + purchases(ind,1);
tax_tot_0 = tax_tot_0 + tax_rates(1)*purchases(ind,1);
elseif purchases(ind,2) == 1
sub_tot_1 = sub_tot_1 + purchases(ind,1);
tax_tot_1 = tax_tot_1 + tax_rates(2)*purchases(ind,1);
elseif purchases(ind,2) == 2
sub_tot_2 = sub_tot_2 + purchases(ind,1);
tax_tot_2 = tax_tot_2 + tax_rates(3)*purchases(ind,1);
end
end
tot = sum([tax_tot_0,tax_tot_1,tax_tot_2,sub_tot_0,sub_tot_1,sub_tot_2]);
format short
disp('subtotal for purchases in tax rate 0')
disp(round(sub_tot_0*100)/100)
disp('Total taxes in category 0')
disp(round(tax_tot_0*100)/100)
disp('subtotal for purchases in tax rate 1')
disp(round(sub_tot_1*100)/100)
disp('Total taxes in category 1')
disp(round(tax_tot_1*100)/100)
disp('subtotal for purchases in tax rate 2')
disp(round(sub_tot_2*100)/100)
disp('Total taxes in category 2')
disp(round(tax_tot_2*100)/100)
disp('Subtotal is')
disp(round(sub_tot*100)/100)
disp('Total is')
disp(round(tot*100)/100);

채택된 답변

Walter Roberson
Walter Roberson 2020년 8월 27일
format short g
format long g
format bank

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

제품


릴리스

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by