4

I would like to convert a floating point variable to a string without losing any precision.

I.e. I would like the string to have the same information as my floating point variable contains, since I use the output for further processing (even if it means that the string will be very long and readable).

To put this more clearly, I would like to have functions for cyclic conversion

var dA = 323423.23423423e4;
var sA = toString(dA);
var dnA = toDouble(sA);

and I would like dnA and dA to be equal

Thanks

PS: Sources on the internet usually talk about how to round strings but I have not found information on exact representation. Also I am not interested in Arbitrary Precision calculations, I just need double precision floating point arithmetic.

wirrbel
  • 3,173
  • 3
  • 26
  • 49
  • Does the string need to be a base-10 representation of the floating point value? – Ted Hopp Dec 07 '12 at 16:33
  • @Hogan - ECMA-262 section 4.3.19 specifies that the JavaScript (rather, EcmaScript) internal number representation is a 64-bit IEEE 754 value. Thus, it isn't hardware dependent. – Ted Hopp Dec 07 '12 at 16:37
  • @TedHopp - Sure but you are only guaranteed 15-17 decimal digits of precision, thus it is easy to construct a string which will be rounded. – Hogan Dec 07 '12 at 17:10
  • 1
    @Hogan - If the round trip was String → Number → String, I'd agree with you. Not every base-10 floating-point string has an exact representation in 64-bit IEEE 754. However, the converse is different: every IEEE 754 floating point number does, in fact, have an _exact_ String representation since it is the sum of a finite set of powers of 2, each of which has a finite representation in base 10. So the round trip Number → String → Number can be done exactly. – Ted Hopp Dec 07 '12 at 17:31
  • @TedHopp - Yes the question is tricky like that. Since the OQ is (in fact) string-> number -> string -> number. eg `var dA = 123456323423.23423423e4; var sA = toString(dA); var dnA = toDouble(sA);` Would have dnA = dA, but not have dA equal the interpreted string "123456323423.23423423e4". Still, I'm deleting my original comment since you are more correct than I am. – Hogan Dec 07 '12 at 19:58
  • @TedHopp Base 10 would be nice is not required though. – wirrbel Dec 10 '12 at 08:17
  • so to speak I would like to serialize the double variable and I am just looking for guarantees that the default (rather) implicit conversion like dA + '' is doing this and is not platform/browser specific. I have encountered truncating problems a lot in my scientific career and make it a point to avoid them ;) – wirrbel Dec 10 '12 at 08:25

2 Answers2

5

Let string arithmetic do the string conversion, and then use parseFloat to get it back to a float:

var dA = 323423.23423423e4;
var sA = dA + '';
var dnA = parseFloat(sA);

Here's a fiddle to see it in action.

Note: All JavaScript numbers are doubles, so you don't need to draw a distinction between doubles and floats. Thus, the only place you'd need to worry about precision loss is in your first line. For example, if you did var dA = 987654321.0123456789 for your first line, it would be equivalent to var dA = 987654321.01234567, and dA would still equal dnA in the end.

N'Bayramberdiyev
  • 5,936
  • 7
  • 27
  • 47
Briguy37
  • 8,342
  • 3
  • 33
  • 53
  • this is what I was thinking, however I was worrying whether this rather implicit conversion preserves exactness. – wirrbel Dec 10 '12 at 08:20
  • @holger: Yes, it will preserve the exactness of the _value_ of `dA`. However, as mentioned in my note, this value will be less precise than what you declared it to be if your declaration contained too many digits. – Briguy37 Dec 10 '12 at 14:07
2

Just dA.toString() and parseFloat(sA) should do it.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592