简体   繁体   中英

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. 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. The parseInt() function parses a string argument and returns an integer. All you have to do is pass the values you are getting from your HTML element. 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

let a2 = Number(p1.value);

Use Number() to convert strings to numbers. 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. 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.

Take a look at the Number() method documentation here. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/Number

Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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