こちらのサイト様を参考にほぼコピペで自動計算表を作成していますが、計算表自体は問題なく動いています。
表示は小数点第三位で四捨五入した数値にしたいのですが、
計算する時には小数点以下全ての数値で計算したいです。
以下のコードは参考サイト様のものから少し変更したものになっていますが、知識不足で動いてはいますが間違っているかもしれません。
php
1<body onload="reCalc();"> 2<table> 3 <thead> 4 <tr><th>商品名</th><th>単価</th><th>単価2</th><th>数量</th><th>合計</th></tr> 5 </thead> 6 <tbody> 7 <tr> 8 <th>リンゴ</th> 9 <td><input type="number" id="tanka_001" name="tanka_001" readonly value="123">円</td> 10 <td><input type="number" id="tanka2_001" name="tanka_001" readonly value="<?php $sample = 123 / 1000;echo number_format($sample,2) ?>">円</td> 11 <td><input type="number" id="suryo_001" name="suryo_001" value="30" onchange="reCalc();">個</td> 12 <td><input type="number" id="yokokei_001" name="yokokei_001" readonly value="0" onchange="reCalc();">円</td> 13 </tr> 14 <tr> 15 <th>バナナ</th> 16 <td><input type="number" id="tanka_002" name="tanka_002" readonly value="456">円</td> 17 <td><input type="number" id="tanka2_002" name="tanka_002" readonly value="<?php $sample2 = 456 / 1000;echo number_format($sample2,2) ?>">円</td> 18 <td><input type="number" id="suryo_002" name="suryo_002" value="20" onchange="reCalc();">個</td> 19 <td><input type="number" id="yokokei_002" name="yokokei_002" readonly value="0" onchange="reCalc();">円</td> 20 </tr> 21 <tr> 22 <th>パイナップル</th> 23 <td><input type="number" id="tanka_003" name="tanka_003" readonly value="789">円</td> 24 <td><input type="number" id="tanka2_003" name="tanka_003" readonly value="<?php $sample3 = 789 / 1000;echo number_format($sample3,2) ?>">円</td> 25 <td><input type="number" id="suryo_003" name="suryo_003" value="10" onchange="reCalc();">個</td> 26 <td><input type="number" id="yokokei_003" name="yokokei_003" readonly value="0" onchange="reCalc();">円</td> 27 </tr> 28 </tbody> 29 <tfoot> 30 <tr> 31 <th colspan="3">総合計</th> 32 <td><input type="number" id="total2" name="total2" readonly value="0">個</td> 33 <td><input type="number" id="total" name="total" readonly value="0">円</td> 34 </tr> 35 </tfoot> 36</table> 37</body>
javascript
1function gyo(obj){return obj.parentElement.parentElement;} 2function tanka(obj){return gyo(obj).querySelectorAll("input[id^=tanka]")[0].value;} 3function tanka2(obj){return gyo(obj).querySelectorAll("input[id^=tanka2]")[0].value;} 4function suryo(obj){return gyo(obj).querySelectorAll("input[id^=suryo]")[0].value;} 5function yokokei(obj){ 6 result = Number(tanka2(obj)) * Number(suryo(obj)); 7 gyo(obj).querySelectorAll("input[id^=yokokei]")[0].value = result.toFixed(2); 8 return result; 9} 10function tatekei(){ 11 prices = Array.from(document.querySelectorAll("input[id^=yokokei]")).map(element => Number(yokokei(element))) ; 12 result = prices.reduce(function(sum,element){ 13 return sum + element ; 14 }); 15 return result; 16} 17function tatekei2(){ 18 prices2 = Array.from(document.querySelectorAll("input[id^=suryo]")).map(element => Number(suryo(element))) ; 19 result = prices2.reduce(function(sum,element){ 20 return sum + element ; 21 }); 22 return result; 23} 24function reCalc(){ 25 document.getElementById("total").value = tatekei().toFixed(2); 26 document.getElementById("total2").value = tatekei2().toFixed(2); 27 return; 28}
上記のコードで現状このように表示されています。
商品名と単価の部分はデータベースから引っ張ってきています。
商品名 | 単価 | 単価2 | 数量 | 合計 |
---|---|---|---|---|
リンゴ | 123円 | 0.12円 | 30個 | 3.60円 |
バナナ | 456円 | 0.46円 | 20個 | 9.20円 |
パイナップル | 789円 | 0.79円 | 10個 | 7.90円 |
総合計 | 60.00個 | 20.70円 |
実際は個数でも金額でもないのですがこうしたいです↓
(総合計はこれだと合ってしまいましたが...)
商品名 | 単価 | 単価2 | 数量 | 合計 |
---|---|---|---|---|
リンゴ | 123円 | 表示は0.12で計算する時は0.123にしたい | 30個 | 3.69円(0.123×30) |
バナナ | 456円 | 表示は0.46で計算する時は0.456にしたい | 20個 | 9.12円(0.456×20) |
パイナップル | 789円 | 表示は0.79で計算する時は0.789にしたい | 10個 | 7.89円(0.789×10) |
総合計 | 60.00個 | 20.70円 |
表示は小数点第三位で四捨五入した数値、計算時は小数点以下全ての数値を使用するにはどうしたら良いのでしょうか?
ヒントでも構いませんのでよろしくお願いいたします。