Js math is broken [duplicate] - javascript

This question already has answers here:
Javascript (+) sign concatenates instead of giving sum of variables
(14 answers)
Closed 5 months ago.
Hi I don't understand that. output should be £112 but it's £1696 because it adds 16 and next to it 96.
let output = 2
let cal = "£" + output * 8 + 96
console.log(cal)

If you start your expression with a string, the + operator is used to concatenate. Try using parentheses:
let output = 2
let cal = "£" + (output * 8 + 96)
It's also nice to use template syntax:
let cal = `£${output * 8 + 96}`

Related

What's so special about adding when it comes to strings and numbers? [duplicate]

This question already has answers here:
How to force JS to do math instead of putting two strings together [duplicate]
(11 answers)
Why does JavaScript handle the plus and minus operators between strings and numbers differently?
(7 answers)
Closed 27 days ago.
All except + do the math. How do I do an actual addition mathematically? Don't ask why I need it. I'm curious.
let first = "2" + 2
let second = 4 + 4
let third = first + second
console.log(third)
logged 228
let first = "2" + 2
let second = 4 + 4
let third = first - second
console.log(third)
logged 14
let first = "2" + 2
let second = 4 + 4
let third = first * second
console.log(third)
logged 176
let first = "2" + 2
let second = 4 + 4
let third = first / second
console.log(third)
logged 2.75

How would I get this function to only return three numbers? [duplicate]

This question already has answers here:
How do you round to 1 decimal place in Javascript?
(24 answers)
Rounding a number to one decimal in javascript [duplicate]
(4 answers)
Round values to one decimal place in javascript [duplicate]
(2 answers)
How to show one decimal place (Without rounding) and show .0 for whole numbers?
(4 answers)
Closed 3 years ago.
**<script>
function roundNum() {
var f = document.getElementById("f").value;
var g = document.getElementById("g").value
var g = g-u
document.getElementById("ng").innerHTML = g
</script>**
I am trying to receive a three-digit answer (example: 95.7, instead of 95.6923531)
You can use the javascript toFixed() method.
Example:
var x = 9.656;
x.toFixed(1); // returns 9.7
x.toFixed(2); // returns 9.66
x.toFixed(4); // returns 9.6560
x.toFixed(6); // returns 9.656000
Try this..
Syntax:
number.toFixed(x)
var num = 95.6923531;
console.log(num.toFixed(1));
console.log(num.toFixed(2));
console.log(num.toFixed(3));

How to short length number in JavaScript [duplicate]

This question already has answers here:
Format number to always show 2 decimal places
(37 answers)
How to round float numbers in javascript?
(19 answers)
Closed 4 years ago.
when I calc this value I got a big number like 48.4979898749,
how convert this to 48.49 ???
You could take the number and apply Number#toFixed with the wanted number of digits.
var valuee = document.getElementById("valueNum"),
result = document.getElementById("res");
valuee.onkeyup = function () {
"use strict";
result.innerHTML = "المبلغ المطلوب " + (valuee.value * 0.0680).toFixed(2) + " دولار";
};
<input type="text" id="valueNum">
<div id="res"></div>

How to make 1 + 1 = 2 instead of 1 + 1 = 11 [duplicate]

This question already has answers here:
How to add two strings as if they were numbers? [duplicate]
(20 answers)
Closed 7 years ago.
I am trying to add the number mathematically, but it keeps adding the number after it.
It takes the id number (begen), then it gets the number inside another div (kacbegen).
var begen = $(this).attr('id');
var kacbegen = $("#math" + begen).text();
var toplam = (kacbegen + 1);
alert(toplam);
However, when doing the math (toplam), it alerts all the numbers.How to add the number mathematically ?
Convert it to number via adding a +:
var toplam = (+kacbegen + 1);
Unary plus (+)
The unary plus operator precedes its operand and evaluates to its operand but attempts to converts it into a number, if it isn't already.
It looks like you're working with Strings (and thus a + b is the concatenation operator) when you want to be working with Number (so x + y would be addition)
Perform your favorite way to cast String to Number, e.g. a unary +x
var kacbegen = +$("#math" + begen).text();
You need to use parseInt to convert kacbegen, which is a String instance, to a Number:
var begen = $(this).attr('id');
var kacbegen = $("#math" + begen).text();
var toplam = (parseInt(kacbegen) + 1);
alert(toplam);
The + operator, when used with a String on either side, will serve as a concatenation, calling Number.prototype.toString on 1.
You need to cast the contents to a number:
var contents = $("#math" + begen).text();
var kacbegen = parseFloat(contents);
You use kacbegen as a string. Please use as a integer use parseInt(kacbegen) + 1

Value keeps adding as string in Javascript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Addition turns into concatenation
Here's what I have...
var srate = Math.round(princ * intr * term * 100) / 100; //works fine
var dasvalue = princ + srate; //doesn't work
document.calc.pay.value = dasvalue;
The "var dasvalue = princ + srate;" adds the two sums up as strings.
100 + 1.4 = 1001.4
What am I doing wrong?
You can use the unary plus operator to cast to type Number, ensuring addition rather than concatenation:
var dasvalue = +princ + +srate;
princ is a string too. You can convert it to a Number with the unary + operator.
If your value in princ comes from an input you need to convert it into a number first.
var dasvalue = Number(princ) + srate;

Categories