javascript Converting Strings to Number Example

javascript Converting Strings to Number Example

Today, We want to share with you javascript Converting Strings to Number Example.
In this post we will show you convert string to number using javascript Example, hear for Convert string to number / Convert number to string in JavaScript we will give you demo and example for implement.
In this post, we will learn about JavaScript string-to-number conversion with an example.

parseFloat is a JavaScript one type of Global Functions as well as parseInt is also a JavaScript one type of Global Functions.

1st : parseFloat (for numeric conversion to a floating-point(integer or float) number) or
2nd : parseInt (for (only integer)string-to-integer conversion).

1) parseInt :

Syntax : parseInt(string,10)<.p>

The most common Global method used to number-convert a string to a (interger)number is parseInt(). It have a takes two parameters,Like string (parameters) and radix(parameters).

2) parseFloat :

Syntax : parseFloat(string)

parseFloat converts from a string to a decimal or floating point number and integer (a decimal).

There are 3 contexts(Type conversion) Objects in (js)JavaScript can be all way type converted to primitives datatype in three main contexts using js:

1) Numeric
2) String
3) Boolean

parseFloat – Examples (new comments in each all line give Examples the conversion results below):

[1]parseFloat('9.45kg')  //Answer : 9.45
[2]parseFloat('98.3')    //Answer : 98.3
[3]parseFloat('099.3')   //Answer : 99.3
[4]parseFloat('0x77.3')  //Answer : 0
[5]parseFloat('.5')      //Answer : 0.5
[6]parseFloat('0.1e6')   //Answer : 100000

parseInt – Examples (new comments in each all line give demo output the conversion results below):

(1)parseInt('198.45')  //ans : 198
(2)parseInt('89')      //ans : 89
(3)parseInt('098',10)  //ans : 98
(4)parseInt('77',8)    //ans : 63  (= 7 + 7*8) //desctibe
(5)parseInt('077')     //ans : 63  (= 7 + 7*8) //desctibe
(6)parseInt('77',16)   //ans : 119 (= 7 + 7*16) //desctibe
(7)parseInt('0x77')    //ans : 119 (= 7 + 7*16) //desctibe
(8)parseInt('099')     //ans : 0 (9 is not an octal digit) //desctibe
(9)parseInt('99',8)    //ans : NaN (0 in very old browsers supported e.g. IE3) //desctibe
(10)parseInt('0.1e6')   //ans : 0 ////desctibe
(11)parseInt('ZZ',36)   //ans :  1295 (= 35 + 35*36) //desctibe

convert a currency string to a float or double using jQuery or Javascript

var currency_type = "$1,100.00";
var ans_number = Number(currency_type.replace(/[^0-9\.]+/g,"")); //formatter
document.write(ans_number);

Converting Between Numbers and Strings

<img src="data:image/gif;" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="<script>" title="<script>" />

String conversion with Example: Conversion, toString,etc..

var data_obj = { 'website_name' : 'Pakainfo.com' };
alert(data_obj); // result - [object Object]
console.log(data_obj); // result : { 'website_name' : 'Pakainfo.com' }


Related FAQ

Here are some more FAQ related to this Article:

  1.   500 internal server error ajax
  2.   JavaScript Redirect URL Example
  3.   Remove Hashtag From URL in React
  4.   javascript remove last comma from string
  5.   PHP Laravel 5.7 Image Upload with Validation Tutorial Example
  6.   Image Zoom In Zoom Out using JQuery
  7.   File upload size validation using Laravel 5.7
  8.   jQuery Trigger Custom Events Example
  9.   Create Animated Scroll To Top Button using jQuery
  10.   JavaScript setInterval and clearInterval function