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

parsing - How to only read digits before decimal in javascript? - Stack Overflow

programmeradmin1浏览0评论

I have script I am using here: /

It's supposed to change the background color of each individual table depending on the variable value. However, it does not work as it is now because the variables have a decimal and % after them. How can I make it only read the first two digits before the decimal, without changing what is displayed on the page. One thing to note is that on my site those are variables displayed, not static numbers.

I have script I am using here: http://jsfiddle/6b2yy/1/

It's supposed to change the background color of each individual table depending on the variable value. However, it does not work as it is now because the variables have a decimal and % after them. How can I make it only read the first two digits before the decimal, without changing what is displayed on the page. One thing to note is that on my site those are variables displayed, not static numbers.

Share Improve this question asked Sep 6, 2012 at 3:10 Jerry VirgoJerry Virgo 191 silver badge4 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Use the parseInt function

parseInt('75.9%',10)
> 75

I guess in your case it would be

score = parseInt($(this).text(), 10);

Demo

Instead of reading the numbers up to the decimal, why not read and round the value properly?

parseInt('75.9%',10);            //-> 75
Math.round(parseFloat('75.9%')); //-> 76
发布评论

评论列表(0)

  1. 暂无评论