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. 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 function
  • 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 MATLAB function. 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 value will output 5.
    matlab round to nearest positive integer value example 1matlab round to nearest positive integer value example 1
    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

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

  • Round down for positive and negative values: 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. 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

If you want to round up in MATLAB, you can use the ceil function. This will round up for a positive value:
ceil example to round up integer value first exampleceil example to round up integer value second example
Likewise, it will also round up for a negative value:
ceil example to round up negative integer value first exampleceil example to round up ngative 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;

    If you want to round to 3 decimal places, you just have to set the variable decimal to 3. If you want to round to 4 decimal places, run the code above for decimal=4. For instance:
    round a value to 3 decimal places

  • Round to any nearest desired value: you can also generalize the code to the nearest desired value. 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;

    This would be:
    round to 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;