Rounding a decimal in MATLAB

How can I round a decimal, e.g. 26,548746540516 to 26,5487 in MATLAB?

+4
source share
2 answers

You can use round as follows

 round(x*10000) / 10000.0 

Alternatively you can use round2

 round2(x,0.0001) round2(x,1e-4) 
+8
source

If this is pure mapping, you can also try sprintf () with formatted output. The syntax for what you want will be sprintf('%.4f',26.548746540516); . You can see where you will need to change the number to a variable, and you can easily change the number of numbers after the decimal number (before using, for example,% 2.4f).

+2
source

Source: https://habr.com/ru/post/1382441/


All Articles