最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery separate decimal number - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited Aug 26, 2010 at 10:47 reko_t 56.4k10 gold badges91 silver badges77 bronze badges asked Aug 26, 2010 at 10:08 SergioSergio 1,2397 gold badges29 silver badges42 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 10

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.

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.

You don't need jQuery for this at all:

var parts = num.toString().split('.');

If you want the values as integers instead of strings, you can just do parseInt(parts[0]) to cast back to integer.

发布评论

评论列表(0)

  1. 暂无评论