I would like to make a general function that will take two parameters of any data type (although they will always be some sort of a number) and then multiply them together, round them down, and return their value as an integer.
I've created a template function, but I would like some feedback regarding the function. Will it work in all cases with values containing short, int, float, decimal, etc? If not how can I change it to do so?
// Header File
class MathFunctions
{
public:
template<typename A, typename B>
static int MultiplyToInt(
const A const& base,
const B const& multiplier);
};
// CPP File
#include "MathFunctions.h"
template<typename A, typename B>
int MathFunctions::MultiplyToInt(
const A const& base,
const B const& multiplier)
{
return static_cast<int>(floor(base * multiplier));
}
// Sample code to run the function
float value = 0.82;
const int result = MathFunctions::MultiplyToInt<int, float>(10, value);
Result should hold the value 8, but if the parameters were that of a decimal, float, int, long, short, etc... it should still be able to work as expected.