public static String toString​(double ddouble v)
Returns a string representationrendering of the double argument.
AllThe characters mentionedbelowoftheresult are alldrawnfromthe ASCII characters.Iftheargumentisset.
Any NaN, theresultwhetherquietorsignaling, is thestringrenderedas"NaN".Otherwise,,regardlessof the resultisastringthatrepresentsthesign bit.
Theinfinities+∞ and magnitude(absolutevalue)oftheargument.Ifthesignisnegative,thefirstcharacteroftheresultis'-'('\u002D');ifthesignispositive,nosigncharacterappearsintheresult.Asforthemagnitudem:Ifmisinfinity,itisrepresentedbythecharacters"Infinity";thus,positiveinfinityproducestheresult-∞arerenderedas"Infinity" and negativeinfinityproducestheresult"-Infinity" .Ifmiszero,itisrepresentedbythecharacters"0.0";thus,,respectively.
Thepositiveand negative zeroproducestheresultzeroesarerenderedas"-0.0" and positivezeroproducestheresult"-0.0" .Ifm,respectively.
Afinitenegativev is greaterthanorequalto10-3butlessthan107,thenitisrepresentedrendered as the integerpartofm,indecimalformwithnoleadingzeroes,followedby'.'('\u002Esign '-),' followed by oneormoredecimaldigitsrepresentingtherenderingof the fractionalpartmagnitude-v.
Afinitepositivevisrenderedintwostages:
Selection of m.Ifmadecimal:Awell-defineddecimaldv is lessthan10-3orgreaterthanorequalselected to 107,thenitrepresentv.
Formattingasastring:Thedecimaldv is representedformattedasastring,eitherinplainor in so-called"computerized scientific notation."Letnbe,dependingonitsvalue.
Adecimalisanumberof the formd×10iforsome(unique integer)integersd>0andi such that disnotamultipleof 10n≤m<10n+1;thenletabe.Theseintegersarethesignificandandtheexponent,respectively,of the mathematicallyexactquotientdecimal.Thelength of mandthedecimalisthe(unique)integernmeeting 10nsothat-1 ≤ ad < 10n.
The magnitudedecimaldvforafinitepositivev is thenrepresenteddefined as follows:
LetRbe the integerpartset of a,asasingledecimaldigit,followedby'.'('\u002E'),followedbydecimaldigitsrepresentingalldecimalsthatroundtovaccordingto the fractionalpartusualround-to-closestrule of a,followedbyIEEE754floating-pointarithmetic.
Letmbe the letter'E'('\u0045'),followedbyarepresentationofnasadecimalinteger,asproducedbythemethodInteger.toString(int).HowmanydigitsmustminimallengthoveralldecimalsinR.
Whenm≥2,letT be printedforthe fractionalpartset of alldecimalsinRwithlengthmora?Theremust.Otherwise,letT be atleastonedigittorepresentthe fractionalpart,andbeyondthatsetofalldecimalsinRwithlength1or2.
Definedv as many,butonlyasmany,moredigitsasareneededtouniquelydistinguishthe argumentvaluefromadjacentvaluesoftypedouble.Thatis,supposedecimalinT that xis closesttov.OriftherearetwosuchdecimalsinT,select the exactmathematicalvaluerepresentedbyonewith the evensignificand(thereisexactlyone).
Letdmust,iandn be the doublevaluenearesttox;oriftwodoublevaluesareequallyclosetox,thensignificand,exponentandlengthofdv,respectively.Further,lete=n+i-1andletd1…dmustn be oneofthemandthe leastsignificantbitusualdecimalexpansion of the significand of.Notethatd1≠0≠dn.
TocreatelocalizedstringrepresentationsSubcasei<0:dvisformattedasd1…dn+i.dn+i+1…dn.Thereareexactly-idigitstotheright of afloating-thedecimal point value,usesubclassesofNumberFormat.Forexample,123×10-1isformattedas12.3.
Returns a string representation of the double argument. All characters mentioned below are ASCII characters.
If the argument is NaN, the result is the string "NaN".
Otherwise, the result is a string that represents the sign and magnitude (absolute value) of the argument. If the sign is negative, the first character of the result is '-' ('\u002D'); if the sign is positive, no sign character appears in the result. As for the magnitude m:
If m is infinity, it is represented by the characters "Infinity"; thus, positive infinity produces the result "Infinity" and negative infinity produces the result "-Infinity".
If m is zero, it is represented by the characters "0.0"; thus, negative zero produces the result "-0.0" and positive zero produces the result "0.0".
If m is greater than or equal to 10-3 but less than 107, then it is represented as the integer part of m, in decimal form with no leading zeroes, followed by '.' ('\u002E'), followed by one or more decimal digits representing the fractional part of m.
If m is less than 10-3 or greater than or equal to 107, then it is represented in so-called "computerized scientific notation." Let n be the unique integer such that 10n ≤ m < 10n+1; then let a be the mathematically exact quotient of m and 10n so that 1 ≤ a < 10. The magnitude is then represented as the integer part of a, as a single decimal digit, followed by '.' ('\u002E'), followed by decimal digits representing the fractional part of a, followed by the letter 'E' ('\u0045'), followed by a representation of n as a decimal integer, as produced by the method Integer.toString(int).
How many digits must be printed for the fractional part of m or a? There must be at least one digit to represent the fractional part, and beyond that as many, but only as many, more digits as are needed to uniquely distinguish the argument value from adjacent values of type double. That is, suppose that x is the exact mathematical value represented by the decimal representation produced by this method for a finite nonzero argument d. Then d must be the double value nearest to x; or if two double values are equally close to x, then d must be one of them and the least significant bit of the significand of d must be 0.
To create localized string representations of a floating-point value, use subclasses of NumberFormat.
Returns a string rendering of the double argument.
The characters of the result are all drawn from the ASCII set.
Any NaN, whether quiet or signaling, is rendered as "NaN", regardless of the sign bit.
The infinities +∞ and -∞ are rendered as "Infinity" and "-Infinity", respectively.
The positive and negative zeroes are rendered as "0.0" and "-0.0", respectively.
A finite negative v is rendered as the sign '-' followed by the rendering of the magnitude -v.
A finite positive v is rendered in two stages:
Selection of a decimal : A well-defined decimal dv is selected to represent v.
Formatting as a string : The decimal dv is formatted as a string, either in plain or in computerized scientific notation, depending on its value.
A decimal is a number of the form d×10i for some (unique) integers d > 0 and i such that d is not a multiple of 10. These integers are the significand and the exponent, respectively, of the decimal. The length of the decimal is the (unique) integer n meeting 10n-1 ≤ d < 10n.
The decimal dv for a finite positive v is defined as follows:
Let R be the set of all decimals that round to v according to the usual round-to-closest rule of IEEE 754 floating-point arithmetic.
Let m be the minimal length over all decimals in R.
When m ≥ 2, let T be the set of all decimals in R with length m. Otherwise, let T be the set of all decimals in R with length 1 or 2.
Define dv as the decimal in T that is closest to v. Or if there are two such decimals in T, select the one with the even significand (there is exactly one).
The (uniquely) selected decimal dv is then formatted.
Let d, i and n be the significand, exponent and length of dv, respectively. Further, let e = n + i - 1 and let d1…dn be the usual decimal expansion of the significand. Note that d1 ≠ 0 ≠ dn.
Case -3 ≤ e < 0: dv is formatted as 0.0…0d1…dn, where there are exactly -(n + i) zeroes between the decimal point and d1. For example, 123 × 10-4 is formatted as 0.0123.
Case 0 ≤ e < 7:
Subcase i ≥ 0: dv is formatted as d1…dn0…0.0, where there are exactly i zeroes between dn and the decimal point. For example, 123 × 102 is formatted as 12300.0.
Subcase i < 0: dv is formatted as d1…dn+i.dn+i+1…dn. There are exactly -i digits to the right of the decimal point. For example, 123 × 10-1 is formatted as 12.3.
Case e < -3 or e ≥ 7: computerized scientific notation is used to format dv. Here e is formatted as by Integer.toString(int).
Subcase n = 1: dv is formatted as d1.0Ee. For example, 1 × 1023 is formatted as 1.0E23.
Subcase n > 1: dv is formatted as d1.d2…dnEe. For example, 123 × 10-21 is formatted as 1.23E-19.