dourunlao1642 2013-02-08 14:17
浏览 53
已采纳

php文本字段将数字转换为小数

l need help to finish my php project. l will like to have a text fields V1,V2,V3 and result/output to accept only decimal numbers (4 decimal place ie 0.0000) or automatically turns numbers to four decimal pace and have a read-only field generate the total sum of the 3 text fields without reloading the form.

eg text field v1+text field v2+text field v3=text field result

the text field should accept on four decimal numbers or automatically turn numbers to four decimal place.

I am using the following sum function;

function sum() 
{
    var TotalLBCReceipts = num("TotalLBCReceipts");
    var TotalKaaseReceipts = num("TotalKaaseReceipts");
    var TakoradiToFactory = num("TakoradiToFactory2");
    var KaaseToFactory = num("KaaseToFactory2");
    var SampleResidue = num("SampleResidue");
    var Sweepings  = num("Sweepings");
    var OrganicCocoa = num("OrganicCocoa2");
    var Confiscated = num("Confiscated");
    var Cuttings = num("Cuttings");
    var r = document.getElementById("GRANDTOTAL1");

    if (r != null) 
    {
         r.value = TotalLBCReceipts + 
                   TotalKaaseReceipts + 
                   TakoradiToFactory +
                   KaaseToFactory +
                   SampleResidue +
                   Sweepings +
                   OrganicCocoa +
                   Confiscated+Cuttings;
     }
}

And grabbing the Values using;

function num(id) 
{
    var e = document.getElementById(id);

    if (e != null) 
    {
        var v = e.value;
        if (/^\d+$/.test(v)) 
        {
            return parseInt(v, 10);
        }
    }

return 0;

}

thanks.

  • 写回答

1条回答 默认 最新

  • dongxu3029 2013-02-08 14:25
    关注

    Google is your friend... As is StackOverflow!!

    You need to use JavaScript here, perhaps as well as PHP...

    Restrict to Numbers with decimal place:

    Restricting input to textbox: allowing only numbers and decimal point

    <HTML>
      <HEAD>
        <SCRIPT language=Javascript>
           <!--
           function isNumberKey(evt)
           {
              var charCode = (evt.which) ? evt.which : event.keyCode;
              if (charCode != 46 && charCode > 31 
                && (charCode < 48 || charCode > 57))
                 return false;
    
              return true;
           }
           //-->
        </SCRIPT>
      </HEAD>
      <BODY>
        <INPUT id="txtChar" onkeypress="return isNumberKey(event)" 
               type="text" name="txtChar">
      </BODY>
    </HTML>
    

    Sum Numbers with readonly field:

    adding the text box values and display it using javascript

    <form name="myFormName">
        <p><input type="text" name="myInputName1" value="25.3"></p>
        <p><input type="text" name="myInputName2" value="14.2"></p>
    </form>
    <div id="total"></div>
    
    <script type="text/javascript>
    
        var total = parseFloat(0, 10);
    
        total += parseFloat(document.myFormName.myInputName1.value, 10);
        total += parseFloat(document.myFormName.myInputName2.value, 10);
    
        document.getElementById("total").innerHTML = "Total is " + total;
    
    </script>
    

    Round to 4 Decimal Places;:

    https://stackoverflow.com/a/7269919/1305169

    Math.round(val*10000)/10000
    

    Hope this helps!

    EDIT:

    You need to change your Code to the following;

    Your sum Function:

    function sum() 
    {
        var TotalLBCReceipts = num("TotalLBCReceipts");
        var TotalKaaseReceipts = num("TotalKaaseReceipts");
        var TakoradiToFactory = num("TakoradiToFactory2");
        var KaaseToFactory = num("KaaseToFactory2");
        var SampleResidue = num("SampleResidue");
        var Sweepings  = num("Sweepings");
        var OrganicCocoa = num("OrganicCocoa2");
        var Confiscated = num("Confiscated");
        var Cuttings = num("Cuttings");
        var r = document.getElementById("GRANDTOTAL1");
    
        if (r != null) 
        {
             r.value = Math.round((
                           TotalLBCReceipts + 
                           TotalKaaseReceipts + 
                           TakoradiToFactory +
                           KaaseToFactory +
                           SampleResidue +
                           Sweepings +
                           OrganicCocoa +
                           Confiscated +
                           Cuttings) * 10000) / 10000;
         }
    }
    

    And your num function:

    function num(id) 
    {
        var e = document.getElementById(id);
    
        if (e != null) 
        {
            return Math.round(e.value*10000)/10000;
        }
    
        return 0;
    }
    

    You can probably just return the plain number from the num Function, however I show it here for completeness!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探