Warning: session_start(): open(/tmp/sess_ef63ca371d93231a44b9c2d1ba3d03ea, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How to Get the Decimal Part of a Number in JavaScript - LearnShareIT

How to Get the Decimal Part of a Number in JavaScript

Get the Decimal Part of a Number in JavaScript

If you are having trouble and have no knowledge about “Get the Decimal Part of a Number in JavaScript”, take a look at our instructions below to get the answer.

Brainstorming Ideas

  1. Convert float number to string then split the decimal part
  2. Minus the integer part
  3. Take the decimal part directly by using the division remainder

Get the Decimal Part of a Number in JS

Method 1: Convert float number to string then split the decimal part

  • Step 1: Use toString() method to convert a number to string
  • Step 2: Use split() method to separate two parts of the string from the dot “.”. The result returns an array
  • Step 3: Take the second element of the array as the decimal part

Code:

// Declare a float number
let x = 3.1415 

// Convert x to string
let str = x.toString()

// Split the string, result is an array
// arr[0] = 3, arr[1] = 1415
let arr = str.split(".")                             
console.log("Decimal part of x is:", arr[1])

Result:

Decimal part of x is: 1415

Method 2: Minus the integer part to get the decimal part 

Step 1: Take the integer part from the float number by using Math.trunc() or Math.floor()

Step 2: Subtract the integer part by the float number to get the decimal part

Code:

let x = 3.1415  
let y = 22.16            
let intPart1 = Math.trunc(x)  // Return integer part of x
let intPart2 = Math.floor(y)  // Round down to the nearest integer of y  
 
console.log("Decimal part of x is:", x - intPart1)
console.log("Decimal part of y is:", y - intPart2)

Result:

Decimal part of x is: 0.14150000000000018
Decimal part of y is: 0.16000000000000014

Method 3: Calculate decimal as the division remainder division  

Compute the remainder by % operator 

Code:

let x = 3.1415         
console.log("Decimal part of x is:", x % 1)

Result:

Decimal part of x is: 0.14150000000000018

Summary

You have been through our tutorial to get the decimal part of a number in JavaScript. I hope you understand the ideas to put into practice effectively.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *