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 Center および File ExchangeFile Operations についてさらに検索

タグ

製品


リリース

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by