Trying to change the amount of decimal points

3 次查看(过去 30 天)
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