Usage of Java parseint () and parsefloat ()

Source: Internet
Author: User

First, parseint ()
The parseint () method first looks at the character at position 0, determines if it is a valid number, and if not, the method returns Nan, and no further action is taken. However, if the character is a valid number, the method will look at the character at position 1 and perform the same test. This process continues until a character is found that is not a valid number, at which point the parseint () converts the string before the character to a number.
For example
If you want to convert the string "1234blue" to an integer, then parseint () returns 1234, because when it detects the character B, it stops the detection process.
The parseint () method also has a base mode that converts binary, octal, hexadecimal, or any other binary string into integers.
The base is specified by the second parameter of the parseint () method, so the hexadecimal value is parsed, and, of course, the parseint () method can be called for binary, octal, or even decimal (the default mode).

If the decimal number contains a leading 0, it is best to use cardinality 10 so that the octal value is not unexpectedly obtained.

All variants of the method:

static int parseint (string s) static int parseint (string s, int radix)

Parameters:

Here are the details of the parameters:

    • String S: This is the string representation of the decimal.

    • int radix: This will be used to convert the string to an integer.



return value:

    • parseint (String s): This returns a integer (decimal only).

    • parseint (int i): This returns an integer, given a string representation of decimal, binary, octal, or hexadecimal (radix e Quals, 2, 8, or respectively) numbers as input.

Instance:

 Public classtest{ Public Static voidMain (String args[]) {intX =integer.parseint ("9"); Doublec = double.parsedouble ("5"); intb = Integer.parseint ("444", -); //by Www.yiibai.com/javaSystem. out. println (x); System. out. println (c); System. out. println (b); }  }  

Output Result:

95.01092

Second, parsefloat ()

Similar to the parseint () method, view each character starting at position 0 until the first non-valid character is found, and then convert the string before the character to a number.
However, for this method, the first decimal point that appears is a valid character. If there are two decimal points, the second decimal point will be considered invalid, parsefloat
The () method converts the string before the decimal point to a number. This means that the string "22.34.5" will be parsed into 22.34.
Another difference between using the Parsefloat () method is that the string must represent a floating-point number in decimal form, not in octal or hexadecimal form.
The method ignores the leading 0, so the octal number 0908 is parsed to 908. For hexadecimal 0xA, the method returns Nan because, in a floating-point number, X is not a valid character.

In addition, parsefloat () has no base mode.

Instance:

Parsefloat ("1234blue"); // return 1234.0   Parsefloat ("22.34.5"); // return 22.34   Parsefloat ("0908"); // return 908   Parsefloat ("blue"); // return NaN  

Usage of Java parseint () and parsefloat ()

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.