admin管理员组

文章数量:1279235

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.

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.

Share Improve this question asked Dec 7, 2012 at 16:29 wirrbelwirrbel 3,2994 gold badges27 silver badges51 bronze badges 7
  • Does the string need to be a base-10 representation of the floating point value? – Ted Hopp Commented Dec 7, 2012 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 Commented Dec 7, 2012 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 Commented Dec 7, 2012 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 Commented Dec 7, 2012 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 ment since you are more correct than I am. – Hogan Commented Dec 7, 2012 at 19:58
 |  Show 2 more ments

2 Answers 2

Reset to default 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.

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

本文标签: Convert Double to String without precision loss in javascriptStack Overflow