admin管理员组

文章数量:1420973

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

本文标签: parsingHow to only read digits before decimal in javascriptStack Overflow