2

如何将零添加到输出中以获得百分比?这是代码:

    var margin = 0;             
    if(total_quantity>1000){margin = .32;}
    else if(total_quantity>575){margin = .35;}
    else if(total_quantity>143){margin = .36;}
    else if(total_quantity>71){margin = .38;}
    else if(total_quantity>36){margin = .40;}
    else{margin = .55;} 
    document.getElementById('margin').value=margin;

0.40 边距的输出始终为 4%。我需要它是 40%

4

2 回答 2

1
var margin = 0;
if (total_quantity > 1000) {
    margin = .32;
} else if (total_quantity > 575) {
    margin = .35;
} else if (total_quantity > 143) {
    margin = .36;
} else if (total_quantity > 71) {
    margin = .38;
} else if (total_quantity > 36) {
    margin = .40;
} else {
    margin = .55;
}
function formatPercentage (decimalNumber) {
    return parseInt(decimalNumber * 100, 10); + "%";
}

document.getElementById('margin').value = formatPercentage(margin);

编辑

你说“但我得到了两个百分号” ......那么这应该是你的formatPercentage功能:

function formatPercentage (decimalNumber) {
    return parseInt(decimalNumber * 100, 10);
}
于 2012-08-29T20:52:29.967 回答
0

在您的帖子中,您可能想说“0.40 边距的输出始终为 0.4%。我需要它为 40%”,对吗?将您的保证金乘以 100,您将获得 40% 而不是 0.4%

于 2012-08-29T20:52:03.040 回答