Hello,
I have an issue with the converting the user input from the xml to two decimal places.
The user can input any number from the xml. But i need to consider the value only if it is double .
Lets say, I have valid range for the double number as { 6.22, 3.44, 7.8, 0, 1, 2}
My question is if the user enters 6.215 => should i round off the input to 6.22 Or should i ignore the value as not valid.
I have googled about the issues in the floating point rounding off and representation issues.
I have written sample program as follows
The issue with the precision function is if i give 6.55 => rounded off number after the precision is not 6.55 but "6.5499999999999998" which is not what i expected !!!
Could the experts throw some useful inputs please ?
Thankyou very much
pdk
I have an issue with the converting the user input from the xml to two decimal places.
The user can input any number from the xml. But i need to consider the value only if it is double .
Lets say, I have valid range for the double number as { 6.22, 3.44, 7.8, 0, 1, 2}
My question is if the user enters 6.215 => should i round off the input to 6.22 Or should i ignore the value as not valid.
I have googled about the issues in the floating point rounding off and representation issues.
I have written sample program as follows
Code:
double precision(double f, int places)
{
double n = std::pow(10.0f, places);
return std::round(f * n) / n;
}
bool isNumeric(const std::string& str, double &d) {
try {
size_t sz(0);
d = std::stod(str, &sz);
return sz == str.size();
}
catch (const std::invalid_argument&) {
// if no conversion could be performed.
return false;
}
catch (const std::out_of_range&) {
// if the converted value would fall out of the range of the result type.
return false;
}
}
int main()
{
double value(6.55);
value = precision(value, 2);
}Could the experts throw some useful inputs please ?
Thankyou very much
pdk