how to Align values
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
%price of each
TV_price = 20000.00;
LAPTOP_price = 40000.00;
SPEAKER_price = 6000.00;
MOUSE_price = 1000.00;
KEYBOARD_price = 3000.00;
%get input from user
TV = input('How many TVs were sold? ');
LAPTOP = input('How many Laptops were sold? ');
SPEAKER = input('How many Speakers were sold? ');
MOUSE = input('How many Computer Mouse were sold? ');
KEYBOARD = input('How many Keyboards were sold? ');
fprintf("\n-----------------------------------------------------\n");
%print bill
%Display
fprintf("\nQTY\tDESCRIPTION\t\tUNIT PRICE\tTOTAL PRICE\n");
fprintf("\n---\t-----------\t\t---------\t---------\n");
fprintf("%d\tTV\t\t%.2f\t%.2f\n",TV,TV_price,TV*TV_price);
fprintf("%d\tLAPTOP\t\t%.2f\t%.2f\n",LAPTOP,LAPTOP_price,LAPTOP*LAPTOP_price);
fprintf("%d\tSPEAKER\t\t%.2f\t\t%.2f\n",SPEAKER,SPEAKER_price,SPEAKER*SPEAKER_price);
fprintf("%d\tMOUSE\t\t%.2f\t\t%.2f\n",MOUSE,MOUSE_price,MOUSE*MOUSE_price);
fprintf("%d\tKEYBOARD\t%.2f\t\t%.2f\n",KEYBOARD,KEYBOARD_price,KEYBOARD*KEYBOARD_price);
total = (TV*TV_price)+(LAPTOP*LAPTOP_price)+(SPEAKER*SPEAKER_price)+(MOUSE*MOUSE_price)+(KEYBOARD*KEYBOARD_price);
tax = (total/100)*8.25;
fprintf("\t\t\t\t\t----------\n");
fprintf("\t\t\tSUBTOTAL\t%.2f\n",total);
fprintf("\t\t\tTAXES\t\t%.2f\n",tax)
fprintf("\t\t\tTOTAL\t\t%.2f\n",total+tax);
1 commentaire
Réponses (1)
Nithin
le 6 Fév 2025
Hi May Tanedo,
You could use “table” for presenting the values in an ordered way.
T = table([TV; LAPTOP; SPEAKER; MOUSE; KEYBOARD], ...
["TV"; "LAPTOP"; "SPEAKER"; "MOUSE"; "KEYBOARD"], ...
[TV_price; LAPTOP_price; SPEAKER_price; MOUSE_price; KEYBOARD_price], ...
[TV_total; LAPTOP_total; SPEAKER_total; MOUSE_total; KEYBOARD_total], ...
'VariableNames', {'QTY', 'DESCRIPTION', 'UNIT_PRICE', 'TOTAL_PRICE'});
If you are not considering to convert the data, use format specifiers instead.
%3d and %10.2f are used for alignment, where the number specifies the minimum width of the field.
%-15s is used to left-align the description with a width of 15 characters.
fprintf("\n%-3s %-15s %-10s %-12s\n", "QTY", "DESCRIPTION", "UNIT PRICE", "TOTAL PRICE");
fprintf("%-3s %-15s %-10s %-12s\n", "---", "-----------", "---------", "-----------");
fprintf("%-3d %-15s %10.2f %12.2f\n", TV, "TV", TV_price, TV * TV_price);
fprintf("%-3d %-15s %10.2f %12.2f\n", LAPTOP, "LAPTOP", LAPTOP_price, LAPTOP * LAPTOP_price);
fprintf("%-3d %-15s %10.2f %12.2f\n", SPEAKER, "SPEAKER", SPEAKER_price, SPEAKER * SPEAKER_price);
fprintf("%-3d %-15s %10.2f %12.2f\n", MOUSE, "MOUSE", MOUSE_price, MOUSE * MOUSE_price);
fprintf("%-3d %-15s %10.2f %12.2f\n", KEYBOARD, "KEYBOARD", KEYBOARD_price, KEYBOARD * KEYBOARD_price);
0 commentaires
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!