Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Added a function to round a number to n decimal places #189

Merged
merged 13 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions include/fiction/utils/round_n_decimal_places.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
// Created by Jan Drewniok on 19.04.23.
//

#ifndef FICTION_ROUND_N_DECIMAL_PLACES_HPP
#define FICTION_ROUND_N_DECIMAL_PLACES_HPP

#include <cmath>
Drewniok marked this conversation as resolved.
Show resolved Hide resolved

namespace fiction
{

/**
* Rounds a number to a specified number of decimal places.
*
* @tparam T the type of the number to round.
* @param number the number to round.
* @param n the number of decimal places to round to.
* @return the number rounded to n decimal places.
*/

Drewniok marked this conversation as resolved.
Show resolved Hide resolved
template <typename T>
T round_n_decimal_places(T number, const uint64_t n)
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
{
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
T factor = std::pow(10, n);
Drewniok marked this conversation as resolved.
Show resolved Hide resolved
return std::round(number * factor) / factor;
}

} // namespace fiction

#endif // FICTION_ROUND_N_DECIMAL_PLACES_HPP
50 changes: 50 additions & 0 deletions test/utils/round_n_decimal_places.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// Created by Jan Drewniok on 19.04.23.
//

#include <catch2/catch_test_macros.hpp>

#include <fiction/utils/round_n_decimal_places.hpp>

using namespace fiction;

TEST_CASE("round_n_decimal_places should round an input number to n decimal places", "[round_n_decimal_places]")
{
SECTION("int64_t")
{
CHECK(round_n_decimal_places(-1LL, 0) == -1LL);
CHECK(round_n_decimal_places(-1LL, 10) == -1LL);
CHECK(round_n_decimal_places(1LL, 0) == 1LL);
CHECK(round_n_decimal_places(1LL, 10) == 1LL);
}

SECTION("double")
{
const double value_positive = 3.145926;
CHECK(round_n_decimal_places(value_positive, 0) == 3);
CHECK(round_n_decimal_places(value_positive, 1) == 3.1);
CHECK(round_n_decimal_places(value_positive, 2) == 3.15);
CHECK(round_n_decimal_places(value_positive, 3) == 3.146);
CHECK(round_n_decimal_places(value_positive, 4) == 3.1459);
CHECK(round_n_decimal_places(value_positive, 5) == 3.14593);
CHECK(round_n_decimal_places(value_positive, 6) == 3.145926);

const double value_negative = -3.145926;
CHECK(round_n_decimal_places(value_negative, 0) == -3);
CHECK(round_n_decimal_places(value_negative, 1) == -3.1);
CHECK(round_n_decimal_places(value_negative, 2) == -3.15);
CHECK(round_n_decimal_places(value_negative, 3) == -3.146);
CHECK(round_n_decimal_places(value_negative, 4) == -3.1459);
CHECK(round_n_decimal_places(value_negative, 5) == -3.14593);
CHECK(round_n_decimal_places(value_negative, 6) == -3.145926);
}

SECTION("Edge cases")
{
CHECK(round_n_decimal_places(1.005, 2) == 1.0);
CHECK(round_n_decimal_places(0.000001, 6) == 0.000001);
CHECK(round_n_decimal_places(0.0000001, 6) == 0);
CHECK(round_n_decimal_places(-0.000001, 6) == -0.000001);
CHECK(round_n_decimal_places(-0.0000001, 6) == 0);
}
}