简体   繁体   English

如何将变量求和为数字?

[英]How to I sum variables as numbers?

Below you can see code I wrote to calculate an area(P) and circumfence(O) of 3 listed geometric shapes.下面你可以看到我编写的代码来计算 3 个列出的几何形状的面积(P)和周长(O)。 The problem I'm encountering is when it calculates circumfence it adds strings together but I want it to add them as numbers.我遇到的问题是当它计算周长时,它会将字符串加在一起,但我希望它将它们作为数字加起来。 Any help on solving this issue is appreciated, also any parts of code you would have done different you can comment.感谢您对解决此问题的任何帮助,也可以评论您可能会做的任何不同的代码部分。 Thanks in advance.提前致谢。 :) :)

   else if (x == 3) {
                    if (p3.value.length == 0) {
                        let a2 = p1.value;
                        let b2 = p2.value;
                        let c2 = Math.sqrt(a2 * a2 + b2 * b2);
                        c2 = c2.toFixed(4);
                        let P33 = a2 * b2 / 2;
                        let O33 = a2 + b2 + c2;
                        document.getElementById("rezultat").innerHTML = "P = " + P33 + "<br> O= " + O33;
                        p3.value = c2;
                    } else if (p2.value.length == 0) {
                        let a2 = p1.value;
                        let c2 = p3.value;
                        let b2 = Math.sqrt(a2 * a2 + c2 * c2);
                        b2 = b2.toFixed(4);
                        let P32 = a2 * b2 / 2;
                        let O32 = a2 + b2 + c2;
                        document.getElementById("rezultat").innerHTML = "P = " + P32 + "<br> O= " + O32;
                        p2.value = b2;
                    } else if (p1.value.length == 0) {
                        let b2 = p2.value;
                        let c2 = p3.value;
                        let a2 = Math.sqrt(b2 * b2 + c2 * c2);
                        a2 = a2.toFixed(4);
                        let P31 = a2 * b2 / 2;
                        let O31 = a2 + b2 + c2;
                        document.getElementById("rezultat").innerHTML = "P = " + P31 + "<br> O= " + O31;
                        p1.value = a2;
                    }

You can use the parseInt() or Number() function.您可以使用parseInt()Number()函数。 The parseInt() function parses a string argument and returns an integer. parseInt()函数解析字符串参数并返回一个整数。 All you have to do is pass the values you are getting from your HTML element.您所要做的就是传递您从 HTML 元素中获取的值。 But if you parse the decimal number, it will be rounded off to the nearest integer value.但是如果你解析十进制数,它将被四舍五入到最接近的整数值。 For eg :例如:

let a2 = parseInt(p1.value);

If you want decimals as well, you can use Number如果你也想要小数,你可以使用Number

let a2 = Number(p1.value);

Use Number() to convert strings to numbers.使用Number()将字符串转换为数字。 Example:例子:

let a2 = Number(p1.value);
let b2 = Number(p2.value);

You could use the Number() method to convert all of your string values to numbers and do your math operations with it.您可以使用 Number() 方法将所有字符串值转换为数字并使用它进行数学运算。 Having this converted only to Integers with the parseInt method is not entirely convenient in this case since you might have decimal numbers as well.在这种情况下,仅使用 parseInt 方法将其转换为整数并不完全方便,因为您可能也有十进制数。

Take a look at the Number() method documentation here.在此处查看 Number() 方法文档。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/Number https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/Number

Cheers!干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM