计算浮点数和精度

我有一项作业要求我计算复利。我做了大部分功课,但我应该显示“存款金额”,其中包含 2 位小数的美分。parseFloat()但是我尝试了一些类似or 的东西,toFixed但我仍然不能以 2 的精度获取结果,我只能使用00.00. 我需要这样的解决方案:

http://img4.mukewang.com/646f227b0001e81002870156.jpg

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="Cersoy-CNIT133-Assignments-4-part2" content="Cersoy-CNIT133-Assignments-4part2">

<meta name='viewport' content='width=device-width, initial-scale=1'>


<link href="../Assignment_all.css" type="text/css" rel="stylesheet">




<title>Cersoy-CNIT133-Assignments_4-Part2</title>

<style>

    #main{

        background-color: burlywood;

        margin:  auto;

        height: 30%;

        width: 50%;

        padding: 1em;

        text-align: center;

        border-radius: 10px;

    }

    #table{

        border: solid 1px black;

    }

    th{

        border: solid 1px black;

    }

    td{

        border: solid 1px black;

    }  

    }

</style>


</head>

<body>

    

    <header>

        Cersoy-CNIT133-Assignments 4-Part2

    </header>

    <div id="main">

       <table id="table1" >

        <tr>

            <th>Year</th>

            <th>Amount of deposit</th>

            <th>Intrest Rate</th>

            

        </tr>

        <table id="table2" >

        <tr>

            <th>Year</th>

            <th>Amount of deposit</th>

            <th>Intrest Rate</th>

        </tr>

        <table id="table3" >

        <tr>

            <th>Year</th>

            <th>Amount of deposit</th>

            <th>Intrest Rate</th>

        </tr>

</table>

<script>

        

        

        

    var rate1=5;rate2=6;rate3=7;

   

     function addRow(a,b,c,t) {

    var table=0;

    if(t==1){table = document.getElementById("table1");}

  

    if(t==2){

    table = document.getElementById("table2");

    }

    else if(t==3){

    table = document.getElementById("table3");

    }

    

    var row = table.insertRow(-1);

    var cell1 = row.insertCell(0);

    var cell2 = row.insertCell(1);

    var cell3 = row.insertCell(2);

    cell1.innerHTML = a;

    cell2.innerHTML = b;

    cell3.innerHTML = c;

    

     }

猛跑小猪
浏览 79回答 2
2回答

忽然笑

我只是添加Math.pow并且它有效,错误就在那里。<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="Cersoy-CNIT133-Assignments-4-part2" content="Cersoy-CNIT133-Assignments-4part2"><meta name='viewport' content='width=device-width, initial-scale=1'><link href="../Assignment_all.css" type="text/css" rel="stylesheet"><title>Cersoy-CNIT133-Assignments_4-Part2</title><style>&nbsp; &nbsp; #main{&nbsp; &nbsp; &nbsp; &nbsp; background-color: rgb(222, 209, 135);&nbsp; &nbsp; &nbsp; &nbsp; margin:&nbsp; auto;&nbsp; &nbsp; &nbsp; &nbsp; height: 30%;&nbsp; &nbsp; &nbsp; &nbsp; width: 50%;&nbsp; &nbsp; &nbsp; &nbsp; padding: 1em;&nbsp; &nbsp; &nbsp; &nbsp; text-align: center;&nbsp; &nbsp; &nbsp; &nbsp; border-radius: 10px;&nbsp; &nbsp; }&nbsp; &nbsp; table{&nbsp; &nbsp; &nbsp; &nbsp; margin:&nbsp; auto;&nbsp; &nbsp; &nbsp; &nbsp; width: 50%;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; th{&nbsp; &nbsp; &nbsp; &nbsp; border: solid 1px black;&nbsp; &nbsp; &nbsp; &nbsp; background-color: maroon;&nbsp; &nbsp; &nbsp; &nbsp; color: white;&nbsp; &nbsp; }&nbsp; &nbsp; td{&nbsp; &nbsp; &nbsp; &nbsp; border: solid 1px black;&nbsp; &nbsp; &nbsp; &nbsp; background-color: rgb(201, 255, 4);&nbsp; &nbsp; &nbsp; &nbsp; color: blue;&nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</style></head><body>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <header>&nbsp; &nbsp; &nbsp; &nbsp; Cersoy-CNIT133-Assignments 4-Part2&nbsp; &nbsp; </header>&nbsp; &nbsp; <div id="main">&nbsp; &nbsp; &nbsp; &nbsp;<table id="table1" >&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Year</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Amount of deposit</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Intrest Rate</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; </table>&nbsp; &nbsp; &nbsp; &nbsp; <table id="table2" >&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Year</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Amount of deposit</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Intrest Rate</th>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; </table>&nbsp; &nbsp; &nbsp; &nbsp; <table id="table3" >&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Year</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Amount of deposit</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Intrest Rate</th>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; </table><script>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; var rate1=5;rate2=6;rate3=7;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;function addRow(a,b,c,t) {&nbsp; &nbsp; var table=0;&nbsp; &nbsp; if(t==1){table = document.getElementById("table1");}&nbsp;&nbsp;&nbsp; &nbsp; if(t==2){&nbsp; &nbsp; table = document.getElementById("table2");&nbsp; &nbsp; }&nbsp; &nbsp; else if(t==3){&nbsp; &nbsp; table = document.getElementById("table3");&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; var row = table.insertRow(-1);&nbsp; &nbsp; var cell1 = row.insertCell(0);&nbsp; &nbsp; var cell2 = row.insertCell(1);&nbsp; &nbsp; var cell3 = row.insertCell(2);&nbsp; &nbsp; cell1.innerHTML = a;&nbsp; &nbsp; cell2.innerHTML = b;&nbsp; &nbsp; cell3.innerHTML = c;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; function calc(rate,tab){&nbsp; &nbsp; &nbsp; &nbsp;var P=1000,A=0,n=0;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;r=rate.toFixed(2)&nbsp; &nbsp; &nbsp; &nbsp;r=r/100;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;for(n=1;n<=10;n++){&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; A=(P*(Math.pow((1+r),n)));&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; var fn,o;&nbsp; &nbsp; &nbsp; &nbsp; o = {style: "currency", currency: "USD",minimumFractionDigits: 2,maximumFractionDigits: 2};&nbsp; &nbsp; &nbsp; &nbsp; fn = A.toLocaleString("en", o);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; addRow(n,fn,r,tab);&nbsp; &nbsp; &nbsp; &nbsp; P=A;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; document.addEventListener("DOMContentLoaded",calc(rate1,"1") +calc(rate2,"2")+calc(rate3,"3"));&nbsp; &nbsp;&nbsp;</script>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; </div>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <form action="hm4.html">&nbsp; &nbsp; &nbsp; &nbsp; <button class="back_button" >Return to Assignment-4 page</button>&nbsp; &nbsp; </form>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<footer>&nbsp; &nbsp; &nbsp; &nbsp;This Webpage designed by Cagatay Ersoy&nbsp;&nbsp; &nbsp;</footer>&nbsp; &nbsp;&nbsp;</body></html>

湖上湖

将用于的选项对象更改.toLocalString()为此:&nbsp;A.toLocaleString(undefined, {&nbsp; minimumFractionDigits: 2,&nbsp; maximumFractionDigits: 2})您的 HTML 中有几个错误,您也应该考虑修复这些错误。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript