MATLAB For Beginners: 20-Minute Video Training Course. (Grab The Training Course and The Cheat Sheet for FREE)

       

In this article, we will tackle the different ways to round numbers in MATLAB. To sum up, you can round down, up, to the nearest integer, and to X decimal places using the floor, ceil, fix, and round MATLAB functions. More specifically, you’ll learn to:

  • Round to the nearest Integer using the round function
  • Round down using the floor and fix functions
  • Round up using the ceil function
  • Round to X decimal places

Round to the Nearest Integer: Round Function

  • Round to the nearest integer for positive and negative values: if you want to round a number to its nearest integer value in MATLAB, you need to use the round function. In short, this will round to the nearest integer for negative and positive values.
  • Round function example: if you take the number 4.3, round will output 4, but if you use the number 4.6, the round function will output 5.
    matlab round to nearest positive integer value example 1matlab round to nearest positive integer value example 1
    Moreover, for values that are as near to their above value as to their below value, it will round toward the furthest integer value from zero:
    matlab round to nearest positive integer value away from zeromatlab round to nearest negative integer value away from zero

Round Down in MATLAB: Floor and Fix

Above all, if you want to round down, there are two ways of doing so, depending on your goals.

  • Round down for positive and negative values: first, if you want to round down for a positive and a negative number, you can use the MATLAB function floor:
    round down floor positive value integerround down floor negative value integer
  • Round down to the closest value toward 0: however, if you want to round a number to its closest integer value toward 0, you can use the fix MATLAB function. In short, this will round down for a positive value but will round up for a negative value:
    round down fix positive value integerround down fix negative value integer

Round Up: the Ceil MATLAB Function

In order to round up in MATLAB, you can use the ceil function. In short, this will round up for a positive value:
ceil example to get to higher positive integer value first exampleceil example to get to higher positive integer value second example
Likewise, it will also round up for a negative value:
ceil example to get to higher negative integer value first exampleceil example to get to higher negative integer value second example

Round to X Decimal Places and to Any Nearest Desired Value

  • Round to X decimal places: you can round to X decimal places with the following code:
    decimal = 3;
    value = round(10^decimal*value)/10^decimal;

    For instance, in order to round to 3 decimal places, you just have to set the variable decimal to 3, and to round to 4 decimal places, run the code above for decimal=4. Also, here is a simple example:
    get a number with 3 decimal places

  • Round to any nearest desired value: you can also generalize the code to the nearest desired value. In other words, it doesn’t have to be an integer. For example, let’s say that you want to round to the nearest fourth:
    nearestValue = 0.25;
    value = round(value/nearestValue)*nearestValue;

    Finally, this would be:
    getting to the fourth nearest value

Key takeaways:

  1. To round down in MATLAB, use:
    • floor to round down toward a smaller integer:
      4.6 -> 4          -4.6 -> -5
    • fix to round down towards 0:
      4.6 -> 4          -4.6 -> -4
  2. To round up in MATLAB, use ceil:
           -4.3 -> -4       -4.6 -> -4
  3. To round to the nearest integer, use round:
           
    4.3 -> 4          4.6 -> 5
  4. To round to X decimal places, use:
    value = round(10^decimal*value)/10^decimal;
  5. To round to the nearest desired value, use:
    value = round(value/nearestValue)*nearestValue;

If you liked this article, here are other articles that you might also like: