Previous Next In my earlier tutorial we have seen how to Convert Integer to String in Java. In this tutorial we will see how to convert Double to String and its vice versa. In Java application many times we need to convert a Double to String and its vice versa. From Java 5, Java was introduced new feature Auto-boxing which automatically converts primitive type to Object type. Convert string to double: Convert « Language Basics « Java. Home; Java; 2D Graphics GUI; 3D. Convert string to double: Convert « Language Basics « Java.
Returns a string representation of the doubleargument. 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 andmagnitude (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 characterappears 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 lessthan 10 7, then it is represented as the integer part ofm, in decimal form with no leading zeroes, followed by'.' ( 'u002E'), followed by one ormore decimal digits representing the fractional part of m. If m is less than 10 -3 or greater than orequal to 10 7, then it is represented in so-called'computerized scientific notation.'
Let n be the uniqueinteger such that 10 n ≤ m. Returns a hexadecimal string representation of thedouble argument. All characters mentioned beloware ASCII characters. If the argument is NaN, the result is the string' NaN'. Otherwise, the result is a string that represents the signand magnitude of the argument. If the sign is negative, thefirst character of the result is ' -'( 'u002D'); if the sign is positive, no signcharacter appears in the result.
As for the magnitude m:. If m is infinity, it is represented by the string'Infinity'; thus, positive infinity produces theresult 'Infinity' and negative infinity producesthe result '-Infinity'. If m is zero, it is represented by the string'0x0.0p0'; thus, negative zero produces the result'-0x0.0p0' and positive zero produces the result'0x0.0p0'.
![String To Double Java String To Double Java](/uploads/1/2/5/4/125400056/540051355.jpg)
If m is a double value with anormalized representation, substrings are used to represent thesignificand and exponent fields. The significand isrepresented by the characters '0x1.' Followed by a lowercase hexadecimal representation of the restof the significand as a fraction.
Trailing zeros in thehexadecimal representation are removed unless all the digitsare zero, in which case a single zero is used. Next, theexponent is represented by 'p' followedby a decimal string of the unbiased exponent as if produced bya call to on theexponent value. If m is a double value with a subnormalrepresentation, the significand is represented by thecharacters '0x0.'
Followed by ahexadecimal representation of the rest of the significand as afraction. Trailing zeros in the hexadecimal representation areremoved. Next, the exponent is represented by'p-1022'.
![How to change a string to double java How to change a string to double java](/uploads/1/2/5/4/125400056/697367022.jpg)
Note that there must be atleast one nonzero digit in a subnormal significand.Examples Floating-point ValueHexadecimal String1.00x1.0p0-1.0-0x1.0p02.00x1.0p13.00x1.8p10.50x1.0p-10.250x1.0p-2Double.MAXVALUE0x1.fffffffffffffp1023Minimum Normal Value0x1.0p-1022Maximum Subnormal Value0x0.fffffffffffffp-1022Double.MINVALUE0x0.001p-1022Parameters: d - the double to be converted. Returns: a hex string representation of the argument. Since: 1.5. valueOf public static valueOf( s)throws. Compares this object against the specified object. The resultis true if and only if the argument is notnull and is a Double object thatrepresents a double that has the same value as thedouble represented by this object. For thispurpose, two double values are considered to bethe same if and only if the method returns the identicallong value when applied to each.Note that in most cases, for two instances of classDouble, d1 and d2, thevalue of d1.equals(d2) is true if andonly ifd1.doubleValue d2.doubleValuealso has the value true.
However, there are twoexceptions:. If d1 and d2 both representDouble.NaN, then the equals methodreturns true, even thoughDouble.NaNDouble.NaN has the valuefalse. If d1 represents +0.0 whiled2 represents -0.0, or vice versa,the equal test has the value false,even though +0.0-0.0 has the value true.This definition allows hash tables to operate properly. Overrides: in class Parameters: obj - the object to compare with. Returns: true if the objects are the same;false otherwise.
See Also:. doubleToLongBits public static long doubleToLongBits(double value). Returns a representation of the specified floating-point valueaccording to the IEEE 754 floating-point 'doubleformat' bit layout.Bit 63 (the bit that is selected by the mask0x000000L) represents the sign of thefloating-point number. Bits62-52 (the bits that are selected by the mask0x7ff000L) represent the exponent.
Bits 51-0(the bits that are selected by the mask0x000fffffffffffffL) represent the significand(sometimes called the mantissa) of the floating-point number.If the argument is positive infinity, the result is0x7ff000L.If the argument is negative infinity, the result is0xfff000L.If the argument is NaN, the result is0x7ff000L.In all cases, the result is a long integer that, whengiven to the method, will produce afloating-point value the same as the argument todoubleToLongBits (except all NaN values arecollapsed to a single 'canonical' NaN value). Parameters: value - a double precision floating-point number. Returns: the bits that represent the floating-point number.
doubleToRawLongBits public static long doubleToRawLongBits(double value). Returns a representation of the specified floating-point valueaccording to the IEEE 754 floating-point 'doubleformat' bit layout, preserving Not-a-Number (NaN) values.Bit 63 (the bit that is selected by the mask0x000000L) represents the sign of thefloating-point number. Bits62-52 (the bits that are selected by the mask0x7ff000L) represent the exponent. Bits 51-0(the bits that are selected by the mask0x000fffffffffffffL) represent the significand(sometimes called the mantissa) of the floating-point number.If the argument is positive infinity, the result is0x7ff000L.If the argument is negative infinity, the result is0xfff000L.If the argument is NaN, the result is the longinteger representing the actual NaN value. Unlike thedoubleToLongBits method,doubleToRawLongBits does not collapse all the bitpatterns encoding a NaN to a single 'canonical' NaNvalue.In all cases, the result is a long integer that,when given to the method, willproduce a floating-point value the same as the argument todoubleToRawLongBits.
Parameters: value - a double precision floating-point number. Returns: the bits that represent the floating-point number.
Since: 1.3. longBitsToDouble public static double longBitsToDouble(long bits). Returns the double value corresponding to a givenbit representation.The argument is considered to be a representation of afloating-point value according to the IEEE 754 floating-point'double format' bit layout.If the argument is 0x7ff000L, the resultis positive infinity.If the argument is 0xfff000L, the resultis negative infinity.If the argument is any value in the range0x7ff001L through0x7fffffffffffffffL or in the range0xfff001L through0xffffffffffffffffL, the result is a NaN. No IEEE754 floating-point operation provided by Java can distinguishbetween two NaN values of the same type with different bitpatterns. Distinct values of NaN are only distinguishable byuse of the Double.doubleToRawLongBits method.In all other cases, let s, e, and m be threevalues that can be computed from the argument:int s = ((bits 63) 0)? 1: -1;int e = (int)((bits 52) & 0x7ffL);long m = (e 0)?(bits & 0xfffffffffffffL).
Compares two Double objects numerically. Thereare two ways in which comparisons performed by this methoddiffer from those performed by the Java language numericalcomparison operators ( =, )when applied to primitive double values:.
Double.NaN is considered by this methodto be equal to itself and greater than all otherdouble values (includingDouble.POSITIVEINFINITY). 0.0d is considered by this method to be greaterthan -0.0d.This ensures that the natural ordering ofDouble objects imposed by this method is consistentwith equals. Specified by: in interface Parameters: anotherDouble - the Double to be compared.
Returns: the value 0 if anotherDouble isnumerically equal to this Double; a valueless than 0 if this Doubleis numerically less than anotherDouble;and a value greater than 0 if thisDouble is numerically greater thananotherDouble. Since: 1.2. compare public static int compare(double d1,double d2).
DescriptionThe method is used to get a String object representing the value of the Number Object.If the method takes a primitive data type as an argument, then the String object representing the primitive data type value is returned.If the method takes two arguments, then a String representation of the first argument in the radix specified by the second argument will be returned. SyntaxFollowing are all the variants of this method −String toStringstatic String toString(int i)ParametersHere is the detail of parameters −.i − An int for which string representation would be returned.Return Value.toString − This returns a String object representing the value of this Integer.toString(int i) − This returns a String object representing the specified integer.Example.