필터 지우기
필터 지우기

Trying to change the amount of decimal points

조회 수: 3 (최근 30일)
Olivia Rose
Olivia Rose 2022년 3월 3일
답변: Star Strider 2022년 3월 3일
This is what I have for the input function
weightArray=[68.6611,8.7929,71.6766,44.1901,76.2861,66.1515,22.6083,36.9491,52.6495,65.6995]
dailyMaterialSum = sum(weightArray,'All')
The output answer is correct, but I need it to only be at 2 decimal points. If I input fprintf('%10.2f \n', dailyMaterialSum) It produces the correct form of the answer, but it isn't considered correct because it's a seperate answer. Where am I supposed to place the fprintf to be correct?
These are the output equations
weightArray = (1 + (100 -1)*(rand (10,1)))'
dailyMaterialSum = MaterialSum(weightArray)

답변 (1개)

Star Strider
Star Strider 2022년 3월 3일
Probably the easiest is to use the round function:
weightArray=[68.6611,8.7929,71.6766,44.1901,76.2861,66.1515,22.6083,36.9491,52.6495,65.6995]
weightArray = 1×10
68.6611 8.7929 71.6766 44.1901 76.2861 66.1515 22.6083 36.9491 52.6495 65.6995
dailyMaterialSum = sum(weightArray,'All')
dailyMaterialSum = 513.6647
format short g
dailyMaterialSum = round(dailyMaterialSum, 2)
dailyMaterialSum =
513.66
format bank
dailyMaterialSum = round(dailyMaterialSum, 2)
dailyMaterialSum =
513.66
Also format to change the Command Window display.
.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by