使用javascript无法获得正确的百分比(%)结果

时间:2014-01-28 05:40:21

标签: javascript

我有简单的javascript工具,其中显示了百分比结果的总和。但我无法得到正确的价值。例如:

它正在显示......

1000%10 = 100这是对的 但是1000%10.2 =它仍然显示100.它应该是102.

Javascript代码:

<script>
jQuery(document).ready(function () {
    $('#pvalue1').change(function () {
        var agentfee = parseInt($('#pvalue1').val(), 10) * parseInt($('#pvalue2').val(), 10) / 100;
        $('#pvalue3').val(agentfee);
        var percentagereduce = parseInt($('#pvalue2').val(), 10) - 0.1;
        var newvalue = parseInt($('#pvalue1').val(), 10) * percentagereduce / 100;
        $('#pvalue4').val(newvalue);
        var takevalue1 = parseInt($('#pvalue3').val(), 10);
        var takevalue2 = parseInt($('#pvalue4').val(), 10);
        var finalvalue = takevalue1 - takevalue2;
        $('#pvalue5').val(finalvalue);
    });
    $('#pvalue2').change(function () {
        var agentfee = parseInt($('#pvalue1').val(), 10) * parseInt($('#pvalue2').val(), 10) / 100;
        $('#pvalue3').val(agentfee);
        var percentagereduce = parseInt($('#pvalue2').val(), 10) - 0.1;
        var newvalue = parseInt($('#pvalue1').val(), 10) * percentagereduce / 100;
        $('#pvalue4').val(newvalue);
        var takevalue1 = parseInt($('#pvalue3').val(), 10);
        var takevalue2 = parseInt($('#pvalue4').val(), 10);
        var finalvalue = takevalue1 - takevalue2;
        $('#pvalue5').val(finalvalue);
    });
});
</script> 

Html部分:

    <table>
    <tr>
        <td>House Sale Price:</td>
        <td>$
            <input name="pvalue1" onkeypress="validate(event)" value="" placeholder=" Enter Sale Price" style="width:140px;" type="number" value="<?=$pvalue1?>" id="pvalue1" size="20" class="required inputfield2" required>
        </td>
    </tr>
    <tr>
        <td>Rate quoted by agent:</td>
        <td>&nbsp;&nbsp;
            <input name="pvalue2" onkeypress="validate(event)" value="0" placeholder=" Percentage" style="width:140px;" type="number" value="<?=$pvalue2?>" id="pvalue2" size="20" class="required inputfield2" required>%</td>
    </tr>
</table>
<input name="doRegister" type="submit" id="doRegister" value="Calculate" style="color:white;font-size:20px;" class="submitbutton">
<div id="resultbox2" class="resultbox1">
    <h2>Results</h2>
    <table>
        <tr>
            <td>Agent Fees:</td>
            <td>$
                <input name="pvalue3" value="0" placeholder="" type="number" value="<?=$pvalue3?>" id="pvalue3" size="10" class="resultfield">
            </td>
        </tr>
        <tr>
            <td>
                <div id='show-me' style='display:none'>
                    <input name="pvalue4" value="0" placeholder="" type="number" value="<?=$pvalue4?>" id="pvalue4" size="10" class="resultfield">
                </div>
            </td>
        </tr>
        <tr>
            <td>Reducing the rate the agent is charging by 0.1% will save you:</td>
            <td>$
                <input name="pvalue5" value="0" placeholder="" type="number" value="<?=$pvalue5?>" id="pvalue5" size="10" class="resultfield">
            </td>
        </tr>
    </table>

2 个答案:

答案 0 :(得分:2)

parseInt仅适用于整数,如果您需要整数和浮点数,则需要使用parseFloat

答案 1 :(得分:0)

parseInt会丢弃数字的小数部分,因为.及其之后的任何内容都不会像整数的一部分。请改为parseFloat