Rounding a Number

The Thunkable ‘round’ function will round a given number to its closest whole number.

However, there are times when you want to round a number to a certain number of decimals.

A common example of this is with money. Taking the Euro as an example, people commonly want to round to the nearest cent instead of the nearest euro.

Here is how to round a number to 2 decimal places:

Let’s use an example.

A product costs €10/kg. It is currently on sale, and costs ⅔ of its normal price.

10 * ⅔ = 6.666666666666……

We want this number rounded to 2 decimal places, which is €6.67.

For this example, we will call the initial sale price 6.666666667.

This number is fed to the above function as givenEuro.

  • givenEuro = 6.666666667.
  • Euro is set to 6.666666667.
  • Cent is set to the round of (100euro). This is the round of (1006.666666667), or the round of (666.6666667), which is 667.
  • Euro is set to cent/100, which is, 667/100 = 6.67. This is correct!

This method can be used to round to any amount of decimal places. Simply replace the 100 with the appropriate number. Using 10 will round to one decimal place, using 1,000 will round to 3 decimal places, etc. Here’s an example.

This function is particularly easy to modify because you can simply set ‘fraction’ to the suitable number.

You can also use this method to round things to the nearest 5 or 10.

Using this method, you can round a number to the nearest 5.

Example, to round 57 to the nearest 5:

Number is set to 57.

Multiple is set to 5.

Number_of_multiples is set to the round of (number / multiple), which is the round of (57 / 5), which is the round of (11.4), which is 11.

Number is set to (number_of_multiples * multiple), which is (11*5), which is 55. This is correct!

Some extra blocks can be added in order to round a number to a given number of decimal places or to a multiple of a given number that has been input by the user. The screens Rounding (Decimal) and Rounding (Multiple) of this app demonstrate how to do this.

The blocks that clean up and verify user input are explained in this post.

7 Likes

Nice, I’ve done this (I could have done it in a single step, I know)

From x.thunkable.com

so you just give it a number

From x.thunkable.com

and it does the work and result

2 Likes