1.

Format numbers as currency string in JavaScript

Answer»

The parseInt() method parses up to the FIRST non-digit and returns the parsed value, whereas Number() converts the string into a number, which can also be a float.

To learn about the difference between parseInt(string) and Number(string) in JAVASCRIPT, let us see an example:

Let’s SAY we have the following value:

20demo

Using parseInt(string), 20 is returned:

<!DOCTYPE html> <html> <body> <script>   document.write(parseInt("20demo")); </script> </body> </html>

The output: 

20 

Using the Number(string), the same value returns NaN:

<!DOCTYPE html> <html> <body> <script>   document.write(Number("20demo")); </script> </body> </html>

 The output:

NaN  

Let us now WORKAROUND for a float value with both Number(string) and parseInt(string): 

<!DOCTYPE html> <html> <body> <script>   document.write(Number("12.99demo"));   document.write("<BR>"+parseInt("12.99demo")); </script> </body> </html>

The output: 

NaN 12


Discussion

No Comment Found