How to calculate and display electricity used in a month using fprintf?

12 ビュー (過去 30 日間)
Spaceman
Spaceman 2024 年 2 月 23 日
編集済み: Spaceman 2024 年 4 月 8 日 3:59
Given: A power company charges 6.6 cents per KWH of electricity.
Find: Write a script that prompts the user for the number of KWH's they used in a month. The script should then calculate the total elecericty charge for the month and display it in dollars, with 2 decimals.
...
I got it figured out :
n = input('How many kWh this month: ');
fprintf('Your charge for 200 kWh will be $%.2f\n', 0.066*n);

採用された回答

Stephen23
Stephen23 2024 年 2 月 23 日
編集済み: Stephen23 2024 年 3 月 21 日
Note that the assignment does not require that you also print the number of kWH, only the charge is required:
n = str2double(input('How many kWH this month: ','s'));
fprintf('Your charge will be $%.2f\n', 0.066*n)
  1 件のコメント
Spaceman
Spaceman 2024 年 2 月 25 日
編集済み: Spaceman 2024 年 3 月 21 日
Eureka! I overcomplicate everything when it comes to MATLAB for some reason. If you put this in an answer I can accept. :)

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2024 年 2 月 23 日
KWH = input('How many KWH this month: ');
BILL = 0.066 * KWH;
fprintf('Your charge for %f KWH will be $%.2f\n', KWH, BILL)
  1 件のコメント
Spaceman
Spaceman 2024 年 2 月 25 日
Genius. Thank you for the time you took to write this out and help me.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFunctions についてさらに検索

タグ

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by