Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert percent into a decimal in html/ javascript

Tags:

javascript

Javascript:

   var validate(s) = s.match ^( 100(?:\.0{1,2})? | 0*?\.\d{1,2} | \d{1,2}(?:\.\d     {1,2})? )% $ != null; 

   var str = value.match(/\/\/%//g);

    if(converted==NaN){
            alert('Input was not a number');
    }
    else if(converted != null) {

            var fracToDecimal = eval (value);

            alert(fracToDecimal);
    }
    else if(converted = str) {

            var percToDecimal = value/100;

            alert(percToDecimal);
    } }
like image 275
user1464427 Avatar asked Jun 18 '12 18:06

user1464427


People also ask

How do I turn a percentage to a decimal?

To convert a percentage to a decimal, divide by 100. So 25% is 25/100, or 0.25. To convert a decimal to a percentage, multiply by 100 (just move the decimal point 2 places to the right). For example, 0.065 = 6.5% and 3.75 = 375%.

How do I convert a number to a percent in C#?

The percent ("P") format specifier is used to multiply a number by 100. It converts the number into a string representing a % (percentage). In the same way, using (“P1”), would only include a single vale after decimal-point.


1 Answers

So you have a string like: 50%? How about:

var percent = "50%";
var result = parseFloat(percent) / 100.0;
like image 83
rfunduk Avatar answered Nov 07 '22 20:11

rfunduk