parseFloat() 함수는 문자열을 분석해 부동소수점 실수로 반환합니다.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
구문
parseFloat(value)
매개변수
value- 변환할 값.
반환 값
주어진 값에서 분석한 부동소수점 수. 분석할 수 없으면 NaN.
설명
parseFloat()은 최상위 함수로 어떠한 객체와도 연결되어 있지 않습니다.
parseFloat()은 매개변수로 주어진 값 분석하고 부동소수점 수를 반환합니다. 기호(+, -), 숫자(0-9), 소수점 또는 지수 이외의 문자를 발견하면, 그 전까지의 결과만 반환하고 문제의 문자와 그 이후는 모두 무시합니다. 위치에 상관하지 않고, 그런 문자를 발견하면 NaN을 반환하는 더 엄격한 방법이 필요하면 Number(value)를 고려하세요.
값이 문자열인데 첫 번째 문자를 숫자로 변환할 수 없는 경우 parseFloat()도 NaN을 반환합니다.
수학적으로 NaN은 어떤 진법에도 속하지 않습니다. isNaN() 함수를 사용해 결과 값이 NaN인지 확인할 수 있습니다. NaN을 다른 값과의 수학 연산에 사용하면 그 결과도 언제나 NaN이 됩니다.
parseFloat()은 Infinity도 분석 및 반환할 수 있습니다. isFinite() 함수를 사용해 결과 값이 유한수(Infinity, -Infinity, NaN이 아닌 수)인지 알 수 있습니다.
parseFloat()은 toString이나 valueOf 메서드를 구현한 객체도 분석할 수 있습니다. 이 때의 결과는 객체의 toString(), valueOf()의 반환 값을 parseFloat()에 전달한 것과 동일합니다.
예제
숫자를 반환하는 경우
아래 예제는 모두 3.14를 반환합니다.
parseFloat(3.14);
parseFloat('3.14');
parseFloat('314e-2');
parseFloat('0.0314E+2');
parseFloat('3.14와 숫자가 아닌 문자들');
var foo = Object.create(null);
foo.toString = function () { return "3.14"; };
parseFloat(foo);
var foo = Object.create(null);
foo.valueOf = function () { return "3.14"; };
parseFloat(foo);
NaN을 반환하는 경우
다음 예제는 NaN을 반환합니다.
parseFloat('FF2');
명세
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. |
| ECMAScript 5.1 (ECMA-262) The definition of 'parseFloat' in that specification. |
Standard | |
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'parseFloat' in that specification. |
Standard | |
| ECMAScript (ECMA-262) The definition of 'parseFloat' in that specification. |
Living Standard |
브라우저 호환성
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
parseFloat | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
Legend
- Full support
- Full support