Thursday, 26 September 2013

Remove Trailing Zeros from a Double Value

Remove Trailing Zeros from a Double Value

I have encountered a problem in a solution that I am trying to achieve,
namely I need to be able to remove trailing zeros from a double value.
For instance, if I were creating two objects that represent a formula, it
would be appropriate to say that the two formulas (2.0 + 7) and (2.000 +
7) are equivalent. That is, (2.0 + 7) == (2.00000 + 7). However, I'm not
sure the best way to approach this is. I'm thinking regular expressions,
but I'm not certain that I can get the outcome I am looking for.
It seems that there would be an issue with resolution in this case. As an
example, the two formulas 2.0 + 7 and 2.000000001 + 7 aren't the same, but
they are extremely close. So my question is, could a regular expression
account for this? Likewise, would a regular expression be the best
approach?

No comments:

Post a Comment