I'm trying to make a simple calculator for circuits. And I can not figure out how to take the answer and limit to only like two numbers after the decimal place.

3 ビュー (過去 30 日間)
The number underlined is wat im trying to limit the size so its not a long repeating with and exponent. I would like it to be "186.66" instead.
  2 件のコメント
Braden
Braden 2024 年 12 月 16 日
%{
Circuit Calculator
%}
problem = input('What type of problem are you trying to solve?\n1 - Resistor Calculator\n2 - Current Calculator');
%This one solves for the total resistance on a curcuit
if problem == 1
RAmount = input('How many resistors are there?');
type = input('What type of curcuit is it? \n1 - Parallel \n2 - Series');
R1 = input('What is the value of R1?');
R2 = input('What is the value of R2?');
if RAmount > 2
R3 = input('What is the value of R3?');
end
if RAmount > 3
R4 = input('What is the value of R4?');
end
if RAmount > 4
R5 = input('What is the value of R5?');
end
if type == 1
if RAmount == 2
TotalResistance = 1 / ((1/R1) + (1/R2));
elseif RAmount == 3
TotalResistance = 1 / ((1/R1) + (1/R2) + (1/R3));
elseif RAmount == 4
TotalResistance = 1 / ((1/R1) + (1/R2) + (1/R3) + (1/R4));
elseif RAmount == 5
TotalResistance = 1 / ((1/R1) + (1/R2) + (1/R3) + (1/R4) + (1/R5));
end
end
if type == 2
if RAmount == 2
TotalResistance = R1 + R2;
elseif RAmount == 3
TotalResistance = R1 + R2 + R3;
elseif RAmount == 4
TotalResistance = R1 + R2 + R3 + R4;
elseif RAmount == 5
TotalResistance = R1 + R2 + R3 + R4 + R5;
end
end
fprintf ('The total resistance is %d \n', TotalResistance);
end
if problem == 2
voltage = input('What is the voltage?');
R1 = input('What is the value of the resistor?');
Current = voltage / R1;
if Current < 1
Current = Current * 1000;
round( Current * .01);
fprintf ('The total current is %d mA', Current);
else
fprintf ('The total current is %d A', Current);
end
end
fprintf()

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

採用された回答

Mike Croucher
Mike Croucher 2024 年 12 月 16 日
編集済み: Mike Croucher 2024 年 12 月 16 日
This page on the documentation will help explain the details: Formatting Text
TotalResistance = 186.66;
fprintf("The total resistance is %.2f\n",TotalResistance)
The total resistance is 186.66

その他の回答 (1 件)

ScottB
ScottB 2024 年 12 月 16 日
"format bank" will also work if you want to places right of the decimal

カテゴリ

Help Center および File ExchangeGet Started with RF PCB Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by