qwe
qwe

Reputation: 179

Rounding a decimal number in MATLAB

How can I round a decimal number like 26,548746540516 to 26,5487 in MATLAB?

Upvotes: 4

Views: 26210

Answers (2)

petrichor
petrichor

Reputation: 6579

You can use round as follows

round(x*10000) / 10000.0

Alternatively, you can use round2

round2(x,0.0001)
round2(x,1e-4) 

Upvotes: 8

AlwaysWrong
AlwaysWrong

Reputation: 526

If it's purely display, you could also try sprintf() with formatted output. The syntax for what you want would be sprintf('%.4f',26.548746540516); You can see where you would need to change the number to a variable, and can easily change how many numbers after the decimal show up(before as well using %2.4f, for example).

Upvotes: 2

Related Questions