Number.Round

D

T

The M Code Behind the Power Query M function Number.Round

What is Number.Round?

Number.Round is a function that is used to round numbers to a specified number of decimal places. The function takes two arguments: the number to be rounded, and the number of decimal places to round to. The function returns the rounded number as a decimal.

The M Code Behind Number.Round

The M code behind the Number.Round function is quite simple. Here is the code:


Number.Round(number as nullable number, digits as number) as nullable number =>

if number is null then

null

else

Number.RoundUp(number Number.Power(10, digits)) / Number.Power(10, digits)


Let's break this code down:

- The function takes in two arguments: 'number' and 'digits'. 'Number' is the number to be rounded, and 'digits' is the number of decimal places to round to.

- The function then checks if the number is null. If the number is null, the function returns null.

- If the number is not null, the function multiplies the number by 10 to the power of the number of decimal places to round to.

- The function then rounds up the result of step 3.

- Finally, the function divides the result of step 4 by 10 to the power of the number of decimal places to round to.

Examples

Let's take a look at some examples to see how the Number.Round function works.

Example 1

Suppose we have the number 3.14159 and we want to round it to 2 decimal places. Here is the M code we would use:


Number.Round(3.14159, 2)


The result of this code would be 3.14.

Example 2

Suppose we have the number 2.71828 and we want to round it to 4 decimal places. Here is the M code we would use:


Number.Round(2.71828, 4)


The result of this code would be 2.7183.

The Number.Round function in Power Query M is a powerful tool that allows users to round numbers to a specified number of decimal places. The M code behind the function is quite simple and easy to understand. By using the Number.Round function, users can easily manipulate and clean their data.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us

    Subject

    Your Name (required)

    Company/Organisation

    Email (required)

    Telephone

    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)