How to convert an int to a float? [duplicate] - javascript

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>');

Related

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 limit decimal digits in JavaScript output? [duplicate]

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.

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

Int to hex string in javascript [duplicate]

This question already has answers here:
How to convert decimal to hexadecimal in JavaScript
(30 answers)
Closed 8 years ago.
I want to convert an number (integer) to a hex string
2 (0x02) to "\x02"
or
62 (0x0062) to "\x62"
How can I do that correctly?
You can use the to string method:
a = 64;
a.toString(16); // prints "40" which is the hex value
a.toString(8); // prints "100" which is the octal value
a.toString(2); // prints "1000000" which is the binary value
Well, it's seems that you want just to concatenate the integer with \x.
If so just to like that:
var number = 62;
var hexStr = '\x' + number.toString(16);
But you have something strange about explaining.
Note: that 62 is not the same as 0x62, 0x62 would be 98.
var converted = "\x" + number.toString(16)

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