How to Convert a Numeric Value into a Percentage (Or) Append Percentage Symbol to a Number

Add percentage sign to dataframe values

You can paste the % sign to df :

df[] <- paste0(as.matrix(df), '%')
df
# V1 V2 V3 V4 V5
#1 2% 28% 0% 3% 14%
#2 32% 16% 1% 20% 0%
#3 4% 36% 32% 26% 31%
#4 14% 38% 16% 9% 40%

How to format numbers as percentage values in JavaScript?

Here is what you need.

var x=150;
console.log(parseFloat(x).toFixed(2)+"%");
x=0;
console.log(parseFloat(x).toFixed(2)+"%");
x=10
console.log(parseFloat(x).toFixed(2)+"%");

Make a number a percentage

A percentage is just:

(number_one / number_two) * 100

No need for anything fancy:

var number1 = 4.954848;
var number2 = 5.9797;

alert(Math.floor((number1 / number2) * 100)); //w00t!

Adding a percentage sign to a number field in Excel

Format these cells with custom format ?\%

How to format a number as percentage in R?

Even later:

As pointed out by @DzimitryM, percent() has been "retired" in favor of label_percent(), which is a synonym for the old percent_format() function.

label_percent() returns a function, so to use it, you need an extra pair of parentheses.

library(scales)
x <- c(-1, 0, 0.1, 0.555555, 1, 100)
label_percent()(x)
## [1] "-100%" "0%" "10%" "56%" "100%" "10 000%"

Customize this by adding arguments inside the first set of parentheses.

label_percent(big.mark = ",", suffix = " percent")(x)
## [1] "-100 percent" "0 percent" "10 percent"
## [4] "56 percent" "100 percent" "10,000 percent"

An update, several years later:

These days there is a percent function in the scales package, as documented in krlmlr's answer. Use that instead of my hand-rolled solution.


Try something like

percent <- function(x, digits = 2, format = "f", ...) {
paste0(formatC(100 * x, format = format, digits = digits, ...), "%")
}

With usage, e.g.,

x <- c(-1, 0, 0.1, 0.555555, 1, 100)
percent(x)

(If you prefer, change the format from "f" to "g".)

How to print a percentage value in python?

format supports a percentage floating point precision type:

>>> print "{0:.0%}".format(1./3)
33%

If you don't want integer division, you can import Python3's division from __future__:

>>> from __future__ import division
>>> 1 / 3
0.3333333333333333

# The above 33% example would could now be written without the explicit
# float conversion:
>>> print "{0:.0f}%".format(1/3 * 100)
33%

# Or even shorter using the format mini language:
>>> print "{:.0%}".format(1/3)
33%

Add percent symbol as int

You cannot add % symbol without changing the type to string. So what you are doing is by far the best way to proceed with ie, you need to cast the int as string and then concatenate the % symbol to it.



Related Topics



Leave a reply



Submit