How can I round to 2 decimal places
7 views (last 30 days)
Show older comments
fernando piedra
on 27 Aug 2020
Answered: Walter Roberson
on 27 Aug 2020
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);
0 Comments
Accepted Answer
More Answers (0)
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!