How can I round to 2 decimal places

9 次查看(过去 30 天)
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