This question already has answers here:
What is the equivalent of NumberFormat in JavaScript or JQuery?
(5 answers)
Closed 7 years ago.
I know there have been many threads on this topic but none seem to answer my question directly. I want to convert a number to two decimal places no matter the length. So 0.5 should turn into 0.50, and 5.3145 should go to 5.31. Most importantly, I also need to keep the number as a number datatype. I know toFixed(2) will create two decimal places, but this also turns the number into a string. If I wrap the toFixed(2) with a parseInt function (e.g. parseInt(amount.toFixed(2))) then 0.5 seems to be converted to "0.50", and then back to 0.5 with out the trailing zero. Adding a second layer of parentheses doesn't solve the problem either. Any ideas?? Thank you in advance!
What you want to do is impossible to do. Numbers do not have trailing zeros. There are no significant digits. So when you need the trailing zero, you need to convert it to a string with toFixed().
You don't need trailing zeros for mathematical computations - so if you are just looking to display it - keep it a string and use the toFixed(2) then when you need to do computations on it - convert with parseFloat
Related
This question already has answers here:
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 24 days ago.
When I execute the JavaScript code 8/(3-8/3), the result is displayed as 23.99999999999999, but I want to fix it to 24.
In order not to round numbers when they are not repeating decimals or when their repetend is not 9 (e.g. do not round 21.835 to 22; do not round 2.979797979797 to 3), how should I solve this problem?
There doesn't seem to be a clear way to resolve this issue without using some sort of rounding function, but there are alternative ways to write this specific equation, according to MathsIsFun
E.g.
8/(1/3)
This question already has answers here:
difference between parseInt() and parseFloat() [duplicate]
(2 answers)
Closed 10 months ago.
If I do the code below and enter a decimal for one or both of the numbers, lets says I use 0.5 and 0.3, I should get 0.2 but I get 0 only. This makes no sense at all to me, it is probably a problem with using prompt but I need to use prompt or a method that is similar to prompt(I'm using sweetalert2 input for the alert). I am okay with using any js libraries.
const x = parseInt(prompt('1'))
const y = parseInt(prompt('2'))
alert(x-y)
I know it is a weird problem, but I don't know how to fix it.
You need to use parseFloat, not parseInt. parseInt is whole numbers only, while parseFloat allows decimal places.
parseFloat('0.9') === 0.9
parseInt('0.9') === 0
This question already has answers here:
Using bitwise OR 0 to floor a number
(7 answers)
Closed 3 years ago.
is there way to add comma to the thousand digits in numbers?
for example if I have 12345 so I want only the 12,345
also if I have 3215579 so I want only 3,215,579
in my code i do :
{TOTAL.toFixed(0).toLocaleString()}
but it give me just number with no comma inside it and i dont understand whats wrong .
You could use...
Math.trunc() (truncate fractional part, also see below)
Math.floor()(round down)
Math.ceil() (round up)
Math.round() (round to nearest integer)
...dependent on how you wanted to remove the decimal.
Math.trunc() isn't supported on all platforms yet (namely IE), but you could easily use a polyfill in the meantime.
Another method of truncating the fractional portion with excellent platform support is by using a bitwise operator (.e.g |0). The side-effect of using a bitwise operator on a number is it will treat its operand as a signed 32bit integer, therefore removing the fractional component. Keep in mind this will also mangle numbers larger than 32 bits.
You can use Math.trunc(). It is also the best way. :)
let a= 123.45
console.log(Math.trunc(a)) // 123
let b = 3215.579
console.log(Math.trunc(b)) // 3215
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc
The best way is to use javascript function toFixed.
suppose you have let a = 123.45;
let b = a.toFixed(0) // will print out 123
To convert to it into comma separated of thousand do ,
b.toLocaleString()
thats it
You can check the docs here : mdn - javascript
Hope it helps. feel free for doubts
This question already has answers here:
How to deal with big numbers in javascript [duplicate]
(3 answers)
Closed 5 years ago.
I have large decimal numbers which I am getting from a request & I want to convert them to string.
So for EG:
I tried all methods converting to string
var r=12311241412412.1241523523523235
r.toString();
r+'';
''+r;
String(r);
//output
'12311241412412.1241'
//what i want
'12311241412412.1241523523523235'
All methods return the decimal numbers upto 4 digits (12311241412412.1241)
but i want all the number till end.
I also tried r.toFixed().toString() but each time the length of decimal numbers change.
What would be easy way to do this?
the problem is that 12311241412412.1241523523523235 in javascript means 12311241412412.125. whatever you do is not gonna work unless you put the whole thing in a string at the first place.
use this instead:
var r = "12311241412412.1241523523523235";
This question already has answers here:
How to avoid scientific notation for large numbers in JavaScript?
(27 answers)
Closed 6 years ago.
I try to get
Math.pow(2,1000);
The result is " 1.2676506002282294e+30 "
I need the number without Euler's number "e+30"
That's scientific notation, not Euler's number.
If you want to show the number without the e+NN part:
convert it to a string
parse the e+NN part
shift the decimal place the appropriate number of digits
return the output as a string
be aware that doing so will lead to inaccurate values for some calculations due to how floating point arithmetic works.
With very large numbers, JavaScript displays them in scientific notation. This is because it is very expensive and unreadable to list them.
For your example, it basically means
1.2676506002282294 * 10 ^ 30
You take the number and then multiply it by 10 to the 30th power.
Calculators often use "E" or "e" like this: 1.8004E+94
6E+5 is the same as 6 × 10^5
To get it without this notation, simply use smaller numbers as the exponent.
Example: Math.pow(2,10)
Mathisfun provides an excellent article on scientific notation. Check it out here
https://www.mathsisfun.com/numbers/scientific-notation.html
Euler's number is a constant that is the base of a natural number. It's an irrational number, meaning its digits go on forever. The first couple digits are 2.7182818284