I am looking to remove the trailing zeros from the double variable and assign the result back to double variable.
Please note that
- I am on C++ 98.
- I am not looking to print out (using snprintf, cout or printf with %g); I am looking to remove the trailing zero and assign the remainder back to the double variable.
Input: double (e.g. 100.250 or 100.1010 or 100.375)
Output: double (100.25, 100.101, 100.375)
e.g
double Price = 100.250;
double convert = TrimZeros(100.250);
// convert = 100.25
- I tried to convert the double to stringstream, and trim the trailing zero of that string. However I am unable to convert it back to double after trimming (with required precision).
e.g.
double Price = 100.250;
ostringstream osPrice;
osPrice << fixed << Price;
string cPrice = TrimString(osPrice.str()); // my function to trim trailing zero of string
//cPrice = "100.25"
// Now When I try to convert cPrice back to double, it adds extra zeros i.e. 100.250000
I am fine with anything, i.e. - A direct function to trim the trailing zeros of double and assign the remainder back to double variable.
OR
- Convert the double to String, trim the zeroes from string and convert it back properly to double.
Kindly help.