26 lines
574 B
C
26 lines
574 B
C
|
std::string Round(const double value, const int decimals)
|
||
|
{
|
||
|
return CleanDecimals(std::round(value * std::pow(10, decimals)) / std::pow(10, decimals));
|
||
|
}
|
||
|
|
||
|
std::string CleanDecimals(const double value)
|
||
|
{
|
||
|
std::string result = std::to_string(value);
|
||
|
const int size = (int)result.size();
|
||
|
for (int i = size - 1; i > 0; i--)
|
||
|
{
|
||
|
if (result[i] != '0' && i < size - 1)
|
||
|
{
|
||
|
result.erase(i + 1, size);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* Check if the last char is a dot */
|
||
|
if (result[result.size() - 1] == '.')
|
||
|
result.erase(result.size() - 1, 1);
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|