I have a javascript number 12.1542. and I want the new string 12.(1542*60) from this string.
How can I get it. Thanks
You could use the modulus operator:
var num = 12.1542;
console.log(num % 1);
However, due to the nature of floating point numbers, you will get a number that is very slightly different. For 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 floating point numbers you will end up with a number that is very slightly different than you may expect.
Input sanity checks aside, this should work:
var str = '12.1542';
var value = Math.floor( str ) + ( 60 * (str - Math.floor( str ) ) );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With