how to Align values

조회 수: 4 (최근 30일)
mary tanedo
mary tanedo 2021년 3월 23일
답변: Nithin 2025년 2월 6일
%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
KSSV
KSSV 2021년 3월 23일
Read about table; this would be a nice way to present values.

댓글을 달려면 로그인하십시오.

답변 (1개)

Nithin
Nithin 2025년 2월 6일
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);

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by