Deprecated: Assigning the return value of new by reference is deprecated in /home/controlz/public_html/blog/wp-settings.php on line 472

Deprecated: Assigning the return value of new by reference is deprecated in /home/controlz/public_html/blog/wp-settings.php on line 487

Deprecated: Assigning the return value of new by reference is deprecated in /home/controlz/public_html/blog/wp-settings.php on line 494

Deprecated: Assigning the return value of new by reference is deprecated in /home/controlz/public_html/blog/wp-settings.php on line 530

Strict Standards: Declaration of Walker_Page::start_lvl() should be compatible with Walker::start_lvl(&$output) in /home/controlz/public_html/blog/wp-includes/classes.php on line 594

Strict Standards: Declaration of Walker_Page::end_lvl() should be compatible with Walker::end_lvl(&$output) in /home/controlz/public_html/blog/wp-includes/classes.php on line 594

Strict Standards: Declaration of Walker_Page::start_el() should be compatible with Walker::start_el(&$output) in /home/controlz/public_html/blog/wp-includes/classes.php on line 594

Strict Standards: Declaration of Walker_Page::end_el() should be compatible with Walker::end_el(&$output) in /home/controlz/public_html/blog/wp-includes/classes.php on line 594

Strict Standards: Declaration of Walker_PageDropdown::start_el() should be compatible with Walker::start_el(&$output) in /home/controlz/public_html/blog/wp-includes/classes.php on line 611

Strict Standards: Declaration of Walker_Category::start_lvl() should be compatible with Walker::start_lvl(&$output) in /home/controlz/public_html/blog/wp-includes/classes.php on line 705

Strict Standards: Declaration of Walker_Category::end_lvl() should be compatible with Walker::end_lvl(&$output) in /home/controlz/public_html/blog/wp-includes/classes.php on line 705

Strict Standards: Declaration of Walker_Category::start_el() should be compatible with Walker::start_el(&$output) in /home/controlz/public_html/blog/wp-includes/classes.php on line 705

Strict Standards: Declaration of Walker_Category::end_el() should be compatible with Walker::end_el(&$output) in /home/controlz/public_html/blog/wp-includes/classes.php on line 705

Strict Standards: Declaration of Walker_CategoryDropdown::start_el() should be compatible with Walker::start_el(&$output) in /home/controlz/public_html/blog/wp-includes/classes.php on line 728

Strict Standards: Redefining already defined constructor for class wpdb in /home/controlz/public_html/blog/wp-includes/wp-db.php on line 306

Deprecated: Assigning the return value of new by reference is deprecated in /home/controlz/public_html/blog/wp-includes/cache.php on line 103

Strict Standards: Redefining already defined constructor for class WP_Object_Cache in /home/controlz/public_html/blog/wp-includes/cache.php on line 425

Deprecated: Assigning the return value of new by reference is deprecated in /home/controlz/public_html/blog/wp-includes/query.php on line 21

Deprecated: Assigning the return value of new by reference is deprecated in /home/controlz/public_html/blog/wp-includes/theme.php on line 623

Strict Standards: Redefining already defined constructor for class WP_Dependencies in /home/controlz/public_html/blog/wp-includes/class.wp-dependencies.php on line 15

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/controlz/public_html/blog/wp-settings.php:472) in /home/controlz/public_html/blog/wp-content/plugins/si-captcha-for-wordpress/si-captcha.php on line 31

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/controlz/public_html/blog/wp-settings.php:472) in /home/controlz/public_html/blog/wp-content/plugins/si-captcha-for-wordpress/si-captcha.php on line 31

Strict Standards: call_user_func_array() expects parameter 1 to be a valid callback, non-static method GoogleSitemapGeneratorLoader::Enable() should not be called statically in /home/controlz/public_html/blog/wp-includes/plugin.php on line 311
Bug en parseInt de Javascript | formatic.cat

Bug en parseInt de Javascript

Estabamos haciendo unos calculos con javascript, y para transformar un string a un valor para asegurarnos que la suma la hace correctamente nos hemos encontrado en un bug un tanto curioso en la funcion parseInt().

Resulta que todo funciona correcto excepto cuando la funcion tiene un 8 o un 9 a transformar, no sabe hacerlo, y devuelve un 0.

Para solucionarlo se debe multiplicar por 1:

var valor = 9;

alert(parseInt(valor));//devuelve 0
alert(parseInt(valor*1));//devuelve 9

Etiquetas: , , ,

5 comentarios para “Bug en parseInt de Javascript”


  1. Deprecated: Function ereg() is deprecated in /home/controlz/public_html/blog/wp-content/plugins/google-analyticator/google-analyticator.php on line 399
    erick dice:

    es verdad una proqueria por ejemplo trata de sumar dos numeros que tengan como decimales .2 y .6

    ejemplo:

    1.3 + 4.6

    devería dar 5.9
    pero el resultado que bota es : 5.8999999999999995

    … alguna solucion me siento cabreado con este resultado :P


  2. Deprecated: Function ereg() is deprecated in /home/controlz/public_html/blog/wp-content/plugins/google-analyticator/google-analyticator.php on line 399
    SKD_EBR dice:

    Es una estupidez…,

    No es un bug, parseInt de javascript toma su parametro como un número en base 8 (Octal), por lo que si intentas hacer parseInt(08) o parseInt(09), arrojará valores erroneos, para informar a parseInt que le daremos parametros en base 10, debemos hacer

    parseInt(x,10);

    Donde x puede ser cualquier número en base 10.

    Informarse bien es no apresurarnos a hablar estupideses.


  3. Deprecated: Function ereg() is deprecated in /home/controlz/public_html/blog/wp-content/plugins/google-analyticator/google-analyticator.php on line 399
    Raúl dice:

    Cuando parseInt() detecta un cero delante, interpreta que el sistema de numeración es octal; parece ser que en este caso, es el problema; de todas formas, si usais el segundo parámetro de parseInt(valor,sist_numeracion), donde sist_numeracion = 10, ya que estamos trabajando en decimal, no dará ningún problema.


  4. Deprecated: Function ereg() is deprecated in /home/controlz/public_html/blog/wp-content/plugins/google-analyticator/google-analyticator.php on line 399
    rjarl dice:

    Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/controlz/public_html/blog/wp-includes/kses.php on line 947

    Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/controlz/public_html/blog/wp-includes/kses.php on line 948

    Hola,
    No es un bug, mas bien es un comportamiento por defecto del lenguaje.

    parseInt en JavaScript espera dos parámetros, (numero, base), por ejemplo:

    var valor=9;
    alert(parseInt(9,10)) //<– En base 10 nos muestra un 9

    Esto funciona siempre bien si defines la base y te aseguro que si fuera un bug del lenguaje muchas aplicaciones online de banca estarían funcionando mal (te puedes imaginar).

    Mira, la documentación de mozilla lo explica fenomenal:
    https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt

    Saludos
    rjarl


  5. Deprecated: Function ereg() is deprecated in /home/controlz/public_html/blog/wp-content/plugins/google-analyticator/google-analyticator.php on line 399
    alete dice:

    levanto esto un segundo porque quiero saber, si tengo una cuenta regresiva 00:00:00 y quiero parsearla, me interesan solamente los segundos, cómo hago? base 60?

Deja un comentario

CAPTCHA Image Audio Version
Reload Image