5. Mai 2010

Javascript - parseInt - Problem

Durch Zufall habe ich festgestellt, das die Javascript-Methode parsteInt(string, radix) eine Zahl als Octal ansieht, wenn der String z.B: "012" lautet, liefert ein parseInt("012") eine 10.

Wenn man ein decimal parsen möchte, sollte man also parseInt("012", 10) aufrufen.


Siehe dazu auch hier http://www.w3schools.com/jsref/jsref_parseInt.asp


JavaScript parseInt() Function

--------------------------------------------------------------------------------
JavaScript Global Functions
--------------------------------------------------------------------------------

Definition and Usage
The parseInt() function parses a string and returns an integer.

The radix parameter is used to specify which numeral system to be used, for example, a radix of 16 (hexadecimal) indicates that the number in the string should be parsed from a hexadecimal number to a decimal number.

If the radix parameter is omitted, JavaScript assumes the following:

•If the string begins with "0x", the radix is 16 (hexadecimal)
•If the string begins with "0", the radix is 8 (octal). This feature is deprecated
•If the string begins with any other value, the radix is 10 (decimal)

Syntax
parseInt(string, radix)










ParameterDescription
stringRequired. The string to be parsed
radixOptional. A number (from 2 to 36) that represents the numeral system to be used


Keine Kommentare: