Java different type character conversion string/int/float/////

Source: Internet
Author: User

1.int & String

int i=5678; String s= "";

int->string:s=i+ ""; or s=string.valueof (i);

String->int:i=integer.parseint (s); or i=integer.valueof (s). Intvalue ();

2.String & Float

String s= ""; Float f=12.21;

string->float:f= float.valueof (s); f = new Float (s); f = float.parsefloat (s);

Float->string:s= f.tostring ();

Summary: Conversions between strings and other types

(1) Conversion of other types to strings

① the string conversion method of the Calling class: X.tostring ();

② Automatic conversion: x+ "";

③ method of using String: string.volueof (X);

////////////////////////////////////////////////////////////////////////////

(2) string as a value, to other types of conversions

The ① is converted to the corresponding wrapper instance before the corresponding method is converted to another type.

For example, the character "32.1" converts the value of a double type to the format: New Float ("32.1"). Doublevalue (). can also be used: double.valueof ("32.1"). Doublevalue ()

② static Parsexxx method

String s = "1";

byte B = byte.parsebyte (s);

Short T = Short.parseshort (s);

int i = Integer.parseint (s);

Long L = Long.parselong (s);

Float f = float.parsefloat (s);

Double d = double.parsedouble (s);

③character getnumericvalue (char-ch) method

Java different type character conversion string/int/float/////

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.