Math - Rounding - Challenge

In this lecture we discuss various ways to round numbers.

Your challenge is to take the number 6.283 and:

  1. Find the Floor of the number
  2. Find the Ceiling of the number
  3. Round the number to both 1 & 2 decimal places.

Pop your answer below and remember to use the spoiler tags.


I’d also like to mention that the code examples in the video are in pseudo-code only. So whilst many languages will have a similar syntax to how I’ve written them, it’s always best to check the documentation for your language of choice - otherwise you may get caught out by some unexpected behaviour.
For example, the System.Math library in C# doesn’t use traditional midpoint rounding and instead uses something commonly known as banker’s rounding, so you have to explicitly change this if you want things to work how you expect.

6
7
6.3
6.28

1 Like

FLOOR: 6
CEILING: 7
1 DECIMAL: 6.3
2 DECIMAL: 6.28

1 Like

6.283
Floor: 6
Ceiling: 7
Rounded to 1 place: 6.3
Rounded to 2 places: 6.28

1 Like

6.283
Floor : 6
Ceiling : 7
Round to 1 place : 6.3
Round to 2 place : 6.28

1 Like

6.283
Floor: 6.0
Ceiling: 7.0
1st decimal: 6.3
2nd decimal: 6.28

1 Like

Floor: 6
Ceiling: 7
Round to 2 decimal points: 6.28
Round to 1 decimal point: 6.3

1 Like

This is the method I used. I’m testing out what I can, using C++ and the UE4 API.

If anyone has a better way or if they are able to use set precision that would be awesome to discover, I wasn’t able to find anything in the Unreal documentation.

void UMathProblems::BeginPlay()
{
Super::BeginPlay();

    float ChallengeOne = 6.283;

UE_LOG(LogTemp, Warning, TEXT("%f"), FMath::FloorToFloat(ChallengeOne))
UE_LOG(LogTemp, Warning, TEXT("%f"), FMath::FloorToFloat(ChallengeOne*10)/10)
UE_LOG(LogTemp, Warning, TEXT("%f"), FMath::FloorToFloat(ChallengeOne*100)/100)

UE_LOG(LogTemp, Warning, TEXT("%f"), FMath::CeilToFloat(ChallengeOne))
UE_LOG(LogTemp, Warning, TEXT("%f"), FMath::CeilToFloat(ChallengeOne*10)/10)
UE_LOG(LogTemp, Warning, TEXT("%f"), FMath::CeilToFloat(ChallengeOne*100)/100)

}

Output Log:

LogTemp: Warning: 6.000000
LogTemp: Warning: 6.200000
LogTemp: Warning: 6.280000
LogTemp: Warning: 7.000000
LogTemp: Warning: 6.300000
LogTemp: Warning: 6.290000

1 Like

Here’s Mah challenge answers:

FLOOR(6.283) = 6
CEIL(6.283) = 7
ROUND(6.283,1) = 6.3
ROUND(6.283,2) = 6.28
1 Like

Floor: 6
Ceil: 7
One Dec: 6.3
Two Dec: 6.28

1 Like

1 Like

FLOOR: 6
CEILING: 7
1 DECIMAL: 6.3
2 DECIMAL: 6.28

1 Like

Just got this course today, looking forward to learning a lot :slight_smile:

Floor: 6
Ceiling: 7
Rounded to 1 decimal place: 6.3
Rounded to 2 decimal places: 6.28

1 Like

Rounded to 1 place : 6.3
Rounded to 2 places : 6.28
Floor : 6
Ceiling : 7

1 Like
// Using NodeJS

let precision = (n, e) => {
    let p = Math.pow( 10, e )
    return Math.round( n * p ) / p
}

let x = 6.283
console.log(`Floor: ${Math.floor(x)}`) // = 6
console.log(`Ciel: ${Math.ceil(x)}`) // = 7
console.log(`Round 1: ${precision(x, 1)}`) // = 6.3
console.log(`Round 2: ${precision(x, 2)}`) // = 6.28
1 Like

6.283
Floor = 6
Ceil = 7
Dec 1 = 6.3
Dec 2 = 6.28

1 Like

Floor = 6
Ceil = 7
1 Decimal = 6.3
2 Decimal = 6.28

1 Like

Floor: 6
Ceil: 7
Round 1: 6.3
Round 2: 6.28

1 Like