Clear Filters
Clear Filters

Trying to change the amount of decimal points

3 views (last 30 days)
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)

Answers (1)

Star Strider
Star Strider on 3 Mar 2022
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.
.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!