Trying to change the amount of decimal points

1 visualización (últimos 30 días)
Olivia Rose
Olivia Rose el 3 de Mzo. de 2022
Respondida: Star Strider el 3 de Mzo. de 2022
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)

Respuestas (1)

Star Strider
Star Strider el 3 de Mzo. de 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.
.

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by