【问题标题】:Java-script: Trying to add decimal places to my variableJava-script:尝试向我的变量添加小数位
【发布时间】:2019-06-21 02:12:36
【问题描述】:

所以我在 slackoverflow 上寻找与我类似的答案,但没有找到与我的问题类似的任何东西。我有一个问题,我下面的变量的小数位没有显示 2 个小数位

1) 最大值(现在可以使用) 2)费用(我遇到的问题)

所以我曾经固定在一个 var Max 上,它可以工作:)。但问题是使用 toFixed,toFixed() 专门将数字转换为字符串。因此,一旦变量 max 已经是一个字符串,再次对其调用 ' toFixed() ' 是一个无效操作。因为当我尝试在“费用”上使用它时

 var max = (principal/2)-fees;

从函数来看,它不起作用。即使我创建了一个新变量,例如:

var Test = fees.toFixed(2)

根据我上面解释的解释并使用https://repl.it/languages 来帮助我,它仍然不起作用。我发现了关于 Math.round(fees) 的信息,如下所示,但它去掉了小数,只显示整数。 ($8 是下面代码中的费用变量。)

有什么方法可以修复费用变量以显示 2 个小数位,或者做我在下面做的事情并为费用创建一个新的 var = 并使用一些我从未听说过的代码。 (仍在学习)谢谢

<script>
    function CollFee(input, principal, fees){

        var max = ( (principal/2)-fees ).toFixed(2);
        var test = Math.round(fees);
        //var input = this.value;
        //alert(input);

        if(input.value > max){
            input.value = '';
            var text = 'Collection Fee is too high, the total of all collection fees can only be 50% of the Original Principal.';
            text = text + ' Current fee on account is $' +test+ '. THE Maximum additional fee allowed is $' +max;
            alert(text);
            //document.getElementById('collfee_<?php echo $dbid; ?>').value = '';
            //input.value = '';

        }
    };
</script>

【问题讨论】:

标签: javascript php web-deployment


【解决方案1】:

有点不清楚,但问题可能是您应该将数字格式化为正确的字符串,在所有计算完成之后(所以在消息创建期间) - 所以这可能是您想要的

function CollFee(input, principal, fees){

  var max = ( (principal/2)-fees );
  var test = Math.round(fees);

  if(input.value > max){
      input.value = '';
      var text = 'Collection Fee is too high, the total of all collection fees can only be 50% of the Original Principal.';
      text =  text + ' Current fee on account is $' 
            + test.toFixed(2) + '. THE Maximum additional fee allowed is $' 
            + max.toFixed(2);
      alert(text);
      //document.getElementById('collfee_<?php echo $dbid; ?>').value = '';
      //input.value = '';

  }
    };
Type number and press enter (principal=101, fee=33) (if you type 100 you will see alert)<br>
<input type="text" onchange="CollFee(this, 101,33)"/>

【讨论】:

    【解决方案2】:

    您应该真正将逻辑与显示分开。 .toFixed 仅应在您完成数学并处理字符串时使用。您可能遇到的第二个问题是(如果我假设正确的话)因为 'input.value' 也是一个字符串,那么将 input.value 与 max 进行比较也会给您不正确的结果。

    试试

    function CollFee(input, principal, fees){
    
        var max = ( (principal/2)-fees );
        var val = parseFloat(input.value)
    
        if(val > max){
            input.value = '';
            var text = 'Collection Fee is too high, the total of all collection fees can only be 50% of the Original Principal.';
            text = text + ' Current fee on account is $' +fee.toFixed(2)+ '. THE Maximum additional fee allowed is $' +max.toFixed(2);
            alert(text);
        }
    };
    

    【讨论】:

    • 感谢您的帮助。但我发布了有效的方法:)。感谢您的帮助
    【解决方案3】:

    .toFixed() 的一个简单替代方法是乘法、向下舍入和除法,例如:

    let num = 65.4321;
    let bigNum = num * 100;
    let wholeNum = Math.floor(bigNum);
    let currencyFormattedNum = wholeNum/100;
    console.log(currencyFormattedNum); // Logs 65.43
    

    您可以在此处找到更多详细信息:https://javascript.info/number

    【讨论】:

      【解决方案4】:

      我非常感谢您在将来解决此问题的所有回答和有用的建议,但是您的方法都不起作用,因为弹出窗口没有出现。我进行了实验,发现这是可行的。只是为了向你们表示感谢。

      <script>
          function CollFee(input, principal, fees){
      
              var Smax = ( (principal/2)-fees ).toFixed(2);
              var max = ( (principal/2)-fees )
              var FeeroundP = fees * 1;
              var Feeround = FeeroundP.toFixed(2);
              //var input = this.value;
              //alert(input);
      
              if(input.value > max){
                  input.value = '';
                  var text = 'Collection Fee is too high, the total of all collection fees can only be 50% of the Original Principal.';
                  text = text + ' Current fee on account is $' +Feeround+ '. THE Maximum additional fee allowed is $' +Smax;
                  alert(text);
                  //document.getElementById('collfee_<?php echo $dbid; ?>').value = '';
                  //input.value = '';
      
              }
          };
      </script>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-11-17
        • 2018-01-28
        • 2023-04-09
        • 2019-06-04
        • 2013-06-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多