[英]My calculations aren't working in jscript
I'm very new to javascript coding and I know enough to structure a function/arguement, but not enough to troubleshoot. 我对javascript编程非常陌生,我了解足够多的知识来构造函数/参数,但不足以进行故障排除。
Here's the portion of my code in question. 这是我所讨论的代码部分。 I'm trying to yield a rounded integer value from a series of calculations, but I can't seem to get it to work.
我正在尝试通过一系列计算得出一个四舍五入的整数值,但是我似乎无法使其正常工作。
If this worked correctly, it would grab the 20000 value, multiply it by 3 [60000], then divide that by 3250 [18.4615...], then round down to the nearest number [18]. 如果此方法正确运行,它将获取20000值,将其乘以3 [60000],然后将其除以3250 [18.4615 ...],然后向下舍入为最接近的数字[18]。 Then it rewrites the text in the browser to 18.
然后,它将浏览器中的文本重写为18。
$(document).ready(function() { var moneyInSeats = $('#moneyInSeats'); var moneyInSeatsValue = moneyInSeats; var moneyMultiplier = 3; if (moneyInSeatsValue.text() != '') { var seatsVisible = ((moneyInSeatsValue.text()) * moneyMultiplier); var seatsPerSection = 3250; var sectionsVisibleExact = seatsVisible / seatsPerSection; var sectionsVisibleRounded = Math.floor(sectionsVisibleExact); moneyInSeats.text(moneyMultiplier); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p id="moneyInSeats" class="hotField">20000</p>
Code/calculation is fine, you were printing wrong value to the UI ( moneyInSeats
) 代码/计算很好,您在UI上打印了错误的值(
moneyInSeats
)
moneyInSeats.text(sectionsVisibleRounded)
Demo 演示版
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <!doctype html> <html> <head> <meta charset="UTF-8"> </head> <body> <p id="moneyInSeats" class="hotField">20000</p> <script> $(document).ready(function() { var moneyInSeats = $('#moneyInSeats'); var moneyInSeatsValue = moneyInSeats var moneyMultiplier = 3; if (moneyInSeatsValue.text() != '') { var seatsVisible = ((moneyInSeatsValue.text()) * moneyMultiplier); var seatsPerSection = 3250; var sectionsVisibleExact = seatsVisible / seatsPerSection; var sectionsVisibleRounded = Math.floor(sectionsVisibleExact) moneyInSeats.text(sectionsVisibleRounded) } }); </script> </body> </html>
You didn't convert the value from moneyInSeatsValue.text()
into a number. 您没有将
moneyInSeatsValue.text()
的值转换为数字。
Use this: 用这个:
parseInt(moneyInSeatsValue.text(),10)
$(document).ready(function() { var moneyInSeats = $('#moneyInSeats'); var moneyInSeatsValue = moneyInSeats; var moneyMultiplier = 3; if (moneyInSeatsValue.text() != '') { var str = "moneyMultiplier: "+moneyMultiplier+'<br>'; var seatsVisible = (parseInt(moneyInSeatsValue.text(),10) * moneyMultiplier); str += 'seatsVisible: '+seatsVisible+'<br>'; var seatsPerSection = 3250; str += 'seatsPerSection: '+seatsPerSection+'<br>'; var sectionsVisibleExact = seatsVisible / seatsPerSection; str += 'sectionsVisibleExact: '+sectionsVisibleExact+'<br>'; var sectionsVisibleRounded = Math.floor(sectionsVisibleExact); str += 'sectionsVisibleRounded: '+sectionsVisibleRounded+'<br>'; moneyInSeats.html(str); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p id="moneyInSeats" class="hotField">20000</p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.