简体   繁体   中英

How to convert a decimal number into percentage using Javascript

I am trying to convert some decimal numbers present in the XML document into percentages using JavaScript but the code seems to be not working. Here is the html text :

fols is <num> .595498</num><br/>
wif isa <num>.0656</num><br/>
dewi is <num>.6949</num><br/>

Now here is the JavaScript I used :

function changeNumbers(){
    for(i=1; i<= document.getElementByTagName("num").length; i++){
        if(1<document.getElementByTagName("num").value<0){
            UNum = document.getElementByTagName("num").value;
            UNum = Math.round(UNum*100) + '&#37;';
            return UNum;


        }
    }
};
document.getElementByTagName("num").innerHTML = changeNumbers();

Demo Here

Can someone please push me in the right direction with the above JavaScript code. Thanks in advance !!

try following :

function changeNumbers(){
var numElements = document.getElementsByTagName("num");
for(i=0; i<= numElements.length; i++){
    var val = parseFloat(numElements[i].innerHTML);
    if(0 < val && val < 1) {
        numElements[i].innerHTML = Math.round(val * 100);
    }
}
};
changeNumbers();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM