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;
Related
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}`
This question already has answers here:
Formatting a number with exactly two decimals in JavaScript
(32 answers)
Closed 5 years ago.
When I am trying to calculate the values
My output has 15 digits in decimal values
Like if x=3
Then in output it is showing
5.196152422706632
But how can I limit it to
5.19615
How to limit decimal digits in output from 15 digits to 5 digits in JavaScript?
Here is my script:
<script>
function myFunction() {
var x = document.getElementById("phase").value;
document.getElementById("demo").innerHTML = "<b>V<sub>L</sub>is</b><br>" + Math.sqrt(3)*x + "volts";
}
</script>
How can I use this:
double number = 0.#############;
DecimalFormat numberFormat = new DecimalFormat("#.#####");
The toFixed method allows you to set the number of digits.
I would use it like this:
document.getElementById("demo").innerHTML = "<b>V<sub>L</sub>is</b><br>" + (Math.sqrt(3) * x).toFixed(5) + "volts";
Btw, java is a completely different language to javascript - you're not using it here
In javascript you can fix the no of digits you want to display after decimal by using the function - toFixed(n).
Here n specifies the no of digits to display after decimal.
<script>
function myFunction() {
var x = document.getElementById("phase").value;
document.getElementById("demo").innerHTML = "<b>V<sub>L</sub>is</b><br>" + (Math.sqrt(3)*x).toFixed(5) + "volts";
}
</script>
In java you can do it like this.
public static void main(String[] args)
{
String value = String.format("%.3f", Math.sqrt(3)*9);
System.out.println("Value with 3 decimals: " + value);
}
In javascript you should check this anwser.
This question already has answers here:
javascript convert int to float
(3 answers)
Closed 6 years ago.
I have an int, say, var myInt = 24 and I need it to display it as a float like this: 24,000. Is it even possible in javascript?
Important: converted float should be a number, not a string
You can use toFixed() to add zeros after the decimal point.
var myInt = 24;
console.log(myInt.toFixed(3));
Use Number.toFixed:
var int = 24
document.write('<pre>' + JSON.stringify(int.toFixed(3), 0, 4) + '</pre>');
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
This question already has answers here:
How to add two strings as if they were numbers? [duplicate]
(20 answers)
Closed 8 years ago.
The following doesn't work as expected:
function sum(x,y){
return x + y;
}
document.getElementById('submit').onclick = function(e)
{
//do someting
e.stopPropagation();
var value1 = document.getElementById('v1').value,
value2 = document.getElementById('v2').value;
var newSum = sum(value1, value2);
console.log(newSum);
}
There is something wrong with the values being picked up. It should return the sum and not "1+2=12"
Change
return x + y;
to
return parseInt(x) + parseInt(y);
You have to convert the values to numbers first, before adding them. If the numbers are floats, you can use parseFloat instead of parseInt.
Edit As suggested by RGraham in the comments, its always better to pass the radix (check parseInt's doc) explicitly. So, the code becomes
return parseInt(x, 10) + parseInt(y, 10);
For more accuracy:
function sum(x,y){
return parseFloat(x) + parseFloat(y);
}
You need to parse your value to an int as all values are passed as string in javascript.
value1 = parseInt(document.getElementById('v1').value, 10);
value2 = parseInt(document.getElementById('v2').value, 10);
use
parseInt
Syntax
var num = parseInt(string, radix);
Parameters
string
The value to parse. If string is not a string, then it is converted to one. Leading whitespace in the string is ignored.
radix
An integer that represents the radix of the above mentioned string. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified.
function sum(x,y){
return parseInt(x,10) + parseInt(y,10);
}