DEV Community

Kate Galushko
Kate Galushko

Posted on • Updated on

JavaScript General Use Full Number Methods and Properties.

Some use full number methods of javascript using in the javascript programming.

TOSTRING

The toString() method returns a number as a string.

const num = 123;

var result = num.toString();
console.log(result);
// output
123

var result1 = (100+23).toString();
console.log(result1);
//output
123
Enter fullscreen mode Exit fullscreen mode

TOEXPONENTIAL

The toExponential() method returns a string, with a number rounded and written using exponential notation.

const num1 = 9.656;
var result2 = num.toExponential(2);

console.log(result2);
// Output
9.66e+0
Enter fullscreen mode Exit fullscreen mode

TOFIXED

The toFixed() method returns a string, with the number written with a specified number of decimals.

// TOFIXED Method 
const num2 = 9.656;
var result3 = num2.toFixed(0);
console.log(result3);
// Output
10

var result4 = num2.toFixed(2);
console.log(result4);
//output 
9.66
Enter fullscreen mode Exit fullscreen mode

TOPRECISION

The toPrecision() method returns a string, with a number written with a specified lengh.

//  TOPRECISION Method 
const num3 = 9.656;
var result5 = num3.toPrecision(2);

console.log(result5);
// output
9.7

var result6 = num3.toPrecision(4);
console.log(result6);
//output
9.656
Enter fullscreen mode Exit fullscreen mode

VALUEOF

The valueOf() method returns a number as a number.

//  VALUEOF Method 
const num4 = 123;
var result7 = num4.valueOf(4);
console.log(result7);
//output
123
Enter fullscreen mode Exit fullscreen mode

NUMBER

The number() method can be used to convert JavaScript variables to numbers.

// Number Method
console.log(Number(true));
console.log(Number(false));
console.log(Number("10"));
console.log(Number("10.23"));
console.log(Number("10,23"));
//oputput
1
0
10
10.23
NaN
Enter fullscreen mode Exit fullscreen mode

PARSEINT

The parseInt() method perses a string and return a whole number. Spaces are allowed. Only the first number is returned.

// ParseInt Method
console.log(parseInt(10));
console.log(parseInt(10.33));
console.log(parseInt(10 20 30));
console.log(parseInt(10 years));
console.log(parseInt(years 10));
// Output
10
10
10
10
NaN
Enter fullscreen mode Exit fullscreen mode

PARSEFLOAT

The parseFloat() method parses a string and return a number. Spaces are allowed. Only the first number is returned.

//   ParseFloat Method

console.log(parseFloat("10"));
console.log(parseFloat("10.33"));
console.log(parseFloat("10 20 30"));
console.log(parseFloat("10 years"));
console.log(parseFloat("years 10"));
// Output
10
10.33
10
10
NaN
Enter fullscreen mode Exit fullscreen mode

If you still have questions related to Javascript you can ask them on devhubby.com.

Best Javascript Books to learn: topminisite.com

Top comments (4)

Collapse
 
matijanovosel profile image
Matija Novosel

Nice article, I'd advise using the markdown syntax highlighting for your code - it makes things a tad nicer, e.g.

const num = 123;

var result = num.toString();
console.log(result);
// output
123

var result1 = (100+23).toString();
console.log(result1);
//output
123
Enter fullscreen mode Exit fullscreen mode

```javascript
const x = 2;
```

Becomes:

const x = 2;
Enter fullscreen mode Exit fullscreen mode
Collapse
 
aliegotha profile image
Kate Galushko

@matijanovosel It looks really nice now. Thank you so much!

Collapse
 
matijanovosel profile image
Matija Novosel

No problem. I didn't know either until recently to tell you the truth, good luck writing new content!

Collapse
 
aliegotha profile image
Kate Galushko

@matijanovosel Thank you so much for pointing it out. I just started my blog here and did not know it does support markdown. I will definitely use this feature in my next posts.