I have this number 0.020928116469517644 And I want to get the percentage, e.g. 2% in this case. How can I do this? Thanks in advance.
Asked
Active
Viewed 121 times
0
-
Why 20% and not 2%? – May 31 '20 at 01:52
-
2Multiply a decimal by 100 to get it as a percent. To get a whole number, you can use parseInt() or toFixed() – Seiyria May 31 '20 at 01:54
-
1It's a decimal point, not comma – Barmar May 31 '20 at 01:56
1 Answers
2
function percentage(x) {
return Math.round(100*x) + '%'
}
answer = percentage(0.020928116469517644)
Also note that in this example, the percentage is 2% and not 20%

Catch22
- 391
- 2
- 5
-
-
Yep, one more question about this, how can I get non-approximated values? – Juan Morelle May 31 '20 at 02:43
-
Depends on how precise you want it. You can always omit Math.round, but then you will get: 2.0928116469517644% – Catch22 May 31 '20 at 02:46
-
For example, if you want 2 decimal digits, use: (Math.round( x * 10000 + Number.EPSILON ) / 100) + '%' – Catch22 May 31 '20 at 02:49