Skip to content

Commit

Permalink
print 3 decimal places
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedAredah committed Mar 20, 2023
1 parent 8df13e2 commit 7fd30e5
Showing 1 changed file with 33 additions and 33 deletions.
66 changes: 33 additions & 33 deletions src/util/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,35 @@ namespace Utils {

using namespace std;

/**
* Powers
*
* @author Ahmed Aredah
* @date 2/28/2023
*
* @param base The base.
* @param exponent The exponent.
*
* @returns A double.
*/
inline double power(double base, int exponent) {
if (exponent == 0) {
return 1.0;
}
else if (exponent < 0) {
return 1.0 / power(base, -exponent);
}
else if (exponent % 2 == 0) {
double temp = power(base, exponent / 2);
return temp * temp;
}
else {
return base * power(base, exponent - 1);
}
}

template<typename T>

template<typename T>
/**
* Convert a plain numeric value to thousand separated value
*
Expand All @@ -36,16 +62,17 @@ namespace Utils {
*
* @tparam T Generic type parameter.
*/
inline std::string thousandSeparator(T n) {
inline std::string thousandSeparator(T n, int decimals = 3) {
// Get the sign of the number and remove it
int sign = (n < 0) ? -1 : 1;
double approx = power((double)10.0, decimals);
n *= sign;
// Get the integer part of the number
long long intPart = (long long)n;
// Check if the fractional part has any value
bool hasFracPart = (n - intPart > 0);
// Get the fractional part of the number and trim it to 2 decimal places
double fracPart = round((n - intPart) * 100.0) / 100.0;
// Get the fractional part of the number and trim it to n decimal places
double fracPart = round((n - intPart) * (approx)) / (approx);
// Create a locale object to represent a specific localization
locale loc("");
// Set the thousand separator according to the localization
Expand All @@ -56,10 +83,10 @@ namespace Utils {
for (int i = intStr.length() - 3; i > 0; i -= 3) {
intStr.insert(i, 1, separator);
}
// Convert the fractional part to a string and trim it to 2 decimal places
// Convert the fractional part to a string and trim it to n decimal places
stringstream stream;
if (hasFracPart) {
stream << fixed << setprecision(2) << fracPart;
stream << fixed << setprecision(decimals) << fracPart;
}
string fracStr = (hasFracPart) ? stream.str().substr(1) : "";
// Combine the integer and fractional parts into a single string
Expand Down Expand Up @@ -225,33 +252,6 @@ namespace Utils {
}).base(), s.end());
return s;
}

/**
* Powers
*
* @author Ahmed Aredah
* @date 2/28/2023
*
* @param base The base.
* @param exponent The exponent.
*
* @returns A double.
*/
inline double power(double base, int exponent) {
if (exponent == 0) {
return 1.0;
}
else if (exponent < 0) {
return 1.0 / power(base, -exponent);
}
else if (exponent % 2 == 0) {
double temp = power(base, exponent / 2);
return temp * temp;
}
else {
return base * power(base, exponent - 1);
}
}
}


Expand Down

0 comments on commit 7fd30e5

Please sign in to comment.