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
Positioning a "Wrapper" Div Underneath a Fixed Navigation Bar
Yui Reset CSS Makes <Strong><Em>This Not Work</Em></Strong>
CSS Regex Selector Match One or Another Condition
Getting Application.CSS to Override Bootstrap
How to Style an HTML Select Box to Omit Drop Down Arrow for Print
CSS Paged Media :Last Page Selector
What Are the Differences Between Display:Box and Display:Flexbox
How to Change Background-Color on Text Links on Hover But Not Image Links
In Less CSS How to Get Less to Watch a Bunch of Files But Compile a Different File When They Change
How to Prevent Child's Content from Stretching Parent's Width
Bootstrap 4 Add More Sizes Spacing
CSS - Div Width Depending on Image Size Above
Ie Background Size Not Working
How to Pass !Important as Parameter/Option for Lesscss Mixins
How to Apply Clippath to a Div with the Clippath Position Being Relative to the Div Position