JavaScript string-to-digital (integer, floating point, binary conversion)

 

The following are examples of using parseFloat () method: 
parseFloat ( " 1234blue " );   // Returns 1234.0 
parseFloat ( " 0xA " );   // Returns NaN3 
parseFloat ( " 22.5 " );   // Returns 22.5 
parseFloat ( " 22.34.5 " );   // returns A 22.34 
parseFloat ( " 0908 " );   // returns A 908 
parseFloat ( " Blue " );   // returns A NaN

When parseInt converting the character string converted into the corresponding integer. After floating-point numbers are not up. 
parseInt ( " 1234blue " );   // Returns 1234 
parseInt ( " 22.5 " );   // Returns 22 is 
parseInt ( " Blue " );   // Returns NaN3 

further comprising converting parseInt when a base model according to a specified binary conversion corresponding to integer conversion, the base is passed by the second parameter. 
parseInt ( " the AF " ,   16 );   // Returns 175 --- 16 hexadecimal 
parseInt ( " 10 " ,   2 );   // Returns 2 2 --------- binary 
parseInt (" 10 " ,   8 );   // returns A 8 8 --------- band 
parseInt ( " 10 " ,   10 );   // returns A 10 ------ 10 decimal 

to note that, such as through the first character of the string 0, it is best to pass the specified base, or unexpectedly turn into octal integer. 
the parseInt ( " 010 " );   // Returns. 8 
the parseInt ( " 010 " ,   . 8 );   // Returns. 8 
the parseInt ( " 010 " ,   10 );   // Returns 10 

2.453255 .toFixed ( 2)   // 2.45 
2.447088 .toFixed ( 2 ) // 2.45 

the parseInt ();   // discard the fractional part of rounded section, corresponding to the rounding, Math.floor (); 
Math.ceil ( . 5 / 2 );     // rounded up, as long as the fractional exist, the integer part + 1'd; 
Math.round ( . 5 / 2 );    // rounded to the nearest integer. 
Math.floor ( . 5 / 2 );    // downwardly rounding, decimal directly remove, no matter how much the fractional part, the integer part is not +1.

 

Reference:
https://www.cnblogs.com/true-true/p/9772286.html

 

 

Guess you like

Origin www.cnblogs.com/sea-stream/p/11100388.html