简体   繁体   English

toFixed(); 只返回.00

[英]toFixed(); is only returning .00

The below code is returning decimal places, but it's not returning values other than .00, when I have my values set to .67, 1.33, 2.67, etc. 下面的代码返回小数位,但是当我的值设置为.67,1.33,2.67等时,它不会返回.00以外的值。

$(function($) {
    $('#CourseMenu select').change(function() {
        var sum = 0;
        $('#CourseMenu select').each(function(idx, elm) {
            sum += parseInt(elm.value, 10);
        });

     $('#total_potential').html(Math.min(sum,72).toFixed(2));
    });
});

Here's where the js is pulling data from... 这是js从中提取数据的地方......

            <legend>Transfer Course Information</legend>
<label for="School Int.">School Int.</label> 
          <input name="School Int." type="text" size="6" maxlength="6" /> &nbsp; 

          <label for="ID">ID</label>
          <input name="ID" type="text" id="ID" size="8" /> &nbsp; 

            <label for="Name">Name</label>
          <input name="Name" type="text" id="Name" size="25" />&nbsp;

          <label for="Grade">Grade</label>
          <input name="Grade" type="text" id="Grade" size="2" />&nbsp; 

          <label for="Credits">Credits</label>
                <select name="Credits" id="Credits">
                    <option value="0">Select Credit</option>
                    <option value="0.67">1 QtrCr.</option>
                    <option value="1.33">2 QtrCr.</option>
                    <option value="2.00">3 QtrCr.</option>
                    <option value="2.67">4 QtrCr.</option>
                    <option value="3.33">5 QtrCr.</option>
                    <option value="4.00">6 QtrCr.</option>
                    <option value="4.67">7 QtrCr.</option>
                    <option value="5.33">8 QrtCr.</option>
                    <option value="6.00">9 QtrCr.</option>
                    <option value="6.67">10 QtrCr.</option>
                    <option value="1">1 SemCr.</option>
                    <option value="2">2 SemCr. </option>
                    <option value="3">3 SemCr.</option>
                    <option value="4">4 SemCr.</option>
                    <option value="5">5 SemCr.</option>
                    <option value="6">6 SemCr.</option>
                    <option value="7">7 SemCr. </option>
                    <option value="8">8 SemCr.</option>
                    <option value="9">9 SemCr.</option>
                    <option value="10">10 SemCr.</option>
                  </select>

          Transferrable
          <input name="COM105" type="checkbox" id="COM1" />

I tried this, but it didn't work... 我尝试过这个,但它不起作用......

$(function($) {
    $('#CourseMenu select').change(function() {
        var sum = 0;
        $('#CourseMenu select').each(function(idx, elm) {
            sum += parseInt(elm.value, 10);
        });

     $('#total_potential').html(Math.min(sum,72).toFixed(2).replace(".00", ""));
    });
});

You're using parseInt() , which discards the fractions. 你正在使用parseInt() ,它会丢弃分数。 Use parseFloat() instead. 请改用parseFloat()

$(function($) {
    $('#CourseMenu select').change(function() {
        var sum = 0;
        $('#CourseMenu select').each(function(idx, elm) {
            sum += parseFloat(elm.value);
        });

     $('#total_potential').html(Math.min(sum,72).toFixed(2));
    });
});

The parseInt method parses strings into integers (whole numbers). parseInt方法将字符串解析为整数整数 )。 One easy way to convert a string to number in JavaScript while maintaining the fractional part is using the unary plus operator: 在保持小数部分的同时在JavaScript中将字符串转换为数字的一种简单方法是使用一元加运算符:

sum += +elm.value;

The advantage/disadvantage of this compared to parseFloat is that it's stricter: if the string starts with numbers and is followed by letters, like "123abc" , this will return a NaN while parseFloat will return the number that could be parsed before the letters. parseFloat相比,它的优势/劣势在于它更严格:如果字符串以数字开头,后跟字母,如"123abc" ,这将返回NaN,而parseFloat将返回可在字母之前解析的数字。

You are calling "parseInt" on the values before your .toFixed() call, which will turn these floating points into ints. 您在.toFixed()调用之前的值上调用“parseInt”,这会将这些浮点转换为整数。 Try "parseFloat" instead and you should be all set. 尝试使用“parseFloat”,你应该全部设置好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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