Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery separate decimal number

If I have decimal numbers like:

18.1234567

5.2345678

-77.7654321

-0.4567891

How can I separate number before and after decimal dot using Jquery and is there any function that can recognize negative value of the selected number?

like image 699
Sergio Avatar asked Nov 23 '25 09:11

Sergio


2 Answers

I'm assuming your numbers are held as decimals, not as strings? In which case, just use the math functions:

Math.floor ( 18.1234567 ) // = 18
18.1234567 - Math.floor ( 18.1234567 ) // = .1234567

And checking for a negative number is just checking if it's < 0.

like image 200
Tim Rogers Avatar answered Nov 24 '25 22:11

Tim Rogers


use javascript split function

var str = '4.5';
var substr = str.split('.');
// substr[0] contains "4"
// substr[1] contains "5"

to see if this negative you can use javascript also :

 nagative = str.indexOf("-");

if its return 1 (not zero) it is a negative number.

like image 35
Haim Evgi Avatar answered Nov 24 '25 23:11

Haim Evgi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!