0

I am looking to remove the trailing zeros from the double variable and assign the result back to double variable.

Please note that

  1. I am on C++ 98.
  2. 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.

Mihir
  • 531
  • 2
  • 10
  • 35
  • 2
    Um... you can't really remove the digits of a number in the way you describe. If you don't want any trailing zeros in a string representation, just remove them if they appear. –  Apr 25 '17 at 11:55
  • 1
    The trailing zeros don't affect the number so why remove them? `1. == 1.0 == 1.00 == 1.000 == ...`. – NathanOliver Apr 25 '17 at 11:56
  • What is the use case here? – CinCout Apr 25 '17 at 11:57

1 Answers1

2

Types like double hold values, not representations. 100, 100.0, and one hundred and 10x10 are all different representations for the same value. So what you are trying to do is a category error that doesn't make any sense.

It's like asking someone who has ten cars if he can have ten cars in base 16.

// Now When I try to convert cPrice back to double, it adds extra zeros i.e. 100.250000

Show how you determined this. Because it's impossible. The value would be one hundred and one quarter. You can represent that any way you want for your own purposes.

When you do int i = 2; is the value in i in base ten? Or is it binary? Or is it in roman numerals? It isn't any of those things. It's the number two. The number of hands you have.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • isnt `double x = 1;` and `int y= 1;` two different representations of the same value? – 463035818_is_not_an_ai Apr 25 '17 at 13:13
  • hm I think my statement isnt in contradiction what you wrote."Types like double hold values, not representations"... and in my case `x` and `y` are two different representations, but I cannot make `x` hold a different representation of the same value, just a different value represented in the same way (ie as `double`). Not sure if this makes any sense.... – 463035818_is_not_an_ai Apr 25 '17 at 13:19
  • wrong on substance. `Types like double hold values, not representations` is plain wrong. They certainly hold representation of values. – SergeyA Apr 25 '17 at 13:53
  • @SergeyA Were that true, you should be able to change the representation, which you can't. For example, a string can be used to hold a representation of a number if you put in it "ten" or "10". But a `double` cannot. If you put the value ten in it, that's what it will hold and you cannot change the representation at all. It holds values *IN* a representation. But what it holds is a value. (Strictly speaking, the same is true of strings. "10" and "ten" are values for a string, but representations of a number.) – David Schwartz Apr 25 '17 at 17:58