Get value after decimal point in javascript

I have javascript number 12.1542. and I want a new line 12. (1542 * 60) from this line.

How can i get this. Thanks

+4
source share
5 answers

You can use the module operator > :

var num = 12.1542; console.log(num % 1); 

However, due to the nature of the floating point numbers, you will get a number that is very slightly different. In the above example, Chrome gives me 0.15419999999999945 .

Another (slightly longer) option would be to use Math.floor , and then subtract the result from the original number:

 var num = 12.1542; console.log(num - Math.floor(num));​ 

Again, due to the nature of the floating point numbers, you will get a number that is slightly different from what you might expect.

+20
source

floor is probably the way to get what you want:

http://www.w3schools.com/jsref/jsref_floor.asp

You can also use ceil

http://www.w3schools.com/jsref/jsref_obj_math.asp

0
source
 (12.1542).toString().replace(/\.(\d+)/, ".($1*60)"); // "12.(1542*60)" 
0
source

Entering a health check aside, this should work:

 var str = '12.1542'; var value = Math.floor( str ) + ( 60 * (str - Math.floor( str ) ) ); 
0
source

Time to comma:

 function toDec(sec){ var itg=Math.floor(sec); sec=(sec-itg)*60; return itg+sec; // OR: return new String(itg+sec); } 
0
source

Source: https://habr.com/ru/post/1401539/


All Articles