Convert decimal to percentage value
How can I convert a given decimal into the respective percentage value with a certain amount of positions q after the comma? The percentage should be rounded with respect to q+1.
For example, I am looking for something like
percentage[position after the comma]{given decimal}
that produces:
percentage[positions-after-comma = 2]{0.12123456}: 12.12%
percentage[positions-after-comma = 4]{0.12123456}: 12.1235%
percentage[positions-after-comma = 4]{2.12123456}: 212.1235%
percentage[positions-after-comma = 1]{0.6789}: 67.9%
percentage[positions-after-comma = 5]{0.6789}: 67.89000%
decimal-number percentage
add a comment |
How can I convert a given decimal into the respective percentage value with a certain amount of positions q after the comma? The percentage should be rounded with respect to q+1.
For example, I am looking for something like
percentage[position after the comma]{given decimal}
that produces:
percentage[positions-after-comma = 2]{0.12123456}: 12.12%
percentage[positions-after-comma = 4]{0.12123456}: 12.1235%
percentage[positions-after-comma = 4]{2.12123456}: 212.1235%
percentage[positions-after-comma = 1]{0.6789}: 67.9%
percentage[positions-after-comma = 5]{0.6789}: 67.89000%
decimal-number percentage
add a comment |
How can I convert a given decimal into the respective percentage value with a certain amount of positions q after the comma? The percentage should be rounded with respect to q+1.
For example, I am looking for something like
percentage[position after the comma]{given decimal}
that produces:
percentage[positions-after-comma = 2]{0.12123456}: 12.12%
percentage[positions-after-comma = 4]{0.12123456}: 12.1235%
percentage[positions-after-comma = 4]{2.12123456}: 212.1235%
percentage[positions-after-comma = 1]{0.6789}: 67.9%
percentage[positions-after-comma = 5]{0.6789}: 67.89000%
decimal-number percentage
How can I convert a given decimal into the respective percentage value with a certain amount of positions q after the comma? The percentage should be rounded with respect to q+1.
For example, I am looking for something like
percentage[position after the comma]{given decimal}
that produces:
percentage[positions-after-comma = 2]{0.12123456}: 12.12%
percentage[positions-after-comma = 4]{0.12123456}: 12.1235%
percentage[positions-after-comma = 4]{2.12123456}: 212.1235%
percentage[positions-after-comma = 1]{0.6789}: 67.9%
percentage[positions-after-comma = 5]{0.6789}: 67.89000%
decimal-number percentage
decimal-number percentage
edited yesterday
asked yesterday
BJPrim
476
476
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can use the siunitx
package:
documentclass{article}
usepackage{siunitx}
newcommandpercentage[2][round-precision = 2]{% default precision: 2
SI[round-mode = places,
scientific-notation = fixed, fixed-exponent = 0,
output-decimal-marker={.}, #1]{#2e2}{percent}%
}
begin{document}
percentage{0.123456} % -> 12.35 %
percentage[round-precision = 3]{0.123456} % -> 12.345 %
end{document}
New contributor
add a comment |
Your values looks wrong. But beside this:
documentclass{article}
usepackage{xfp,xparse}
ExplSyntaxOn
int_new:Nl_bjprim_round_int
keys_define:nn {bjprim}
{
positions-after-comma .int_set:N = l_bjprim_round_int
}
NewDocumentCommandpercentage { O{} m }
{
keys_set:nn {bjprim}{positions-after-comma=2,#1}
fpeval{round(#2*100,l_bjprim_round_int)}%
}
ExplSyntaxOff
begin{document}
percentage[positions-after-comma = 2]{0.123456}: 12.12%
percentage[positions-after-comma = 4]{0.123456}: 12.1235%
percentage[positions-after-comma = 4]{2.123456}: 212.1235%
percentage[positions-after-comma = 1]{0.6789}: 67.9%
percentage[positions-after-comma = 5]{0.6789}: 67.89000%
end{document}
If you really want to fill up with zeros you could round with siunitx:
documentclass{article}
usepackage{xfp,xparse,siunitx}
ExplSyntaxOn
int_new:Nl_bjprim_round_int
keys_define:nn {bjprim}
{
positions-after-comma .int_set:N = l_bjprim_round_int
}
NewDocumentCommandpercentage { O{} m }
{
keys_set:nn {bjprim}{positions-after-comma=2,#1}
num[round-mode = places,round-precision=l_bjprim_round_int]{fpeval{#2*100}}%
}
ExplSyntaxOff
begin{document}
percentage[positions-after-comma = 2]{0.123456}: 12.12%
percentage[positions-after-comma = 4]{0.123456}: 12.1235%
percentage[positions-after-comma = 4]{2.123456}: 212.1235%
percentage[positions-after-comma = 1]{0.6789}: 67.9%
percentage[positions-after-comma = 5]{0.6789}: 67.89000%
end{document}
This way the optional argument is actually mandatory.
– egreg
yesterday
@egreg thanks, I changed to O{}.
– Ulrike Fischer
yesterday
I am struggling to get your code running. It says: "File `xfp.sty' not found." I am working with Overleaf, can it be that this package is not supported?
– BJPrim
yesterday
That's not what I meant; if you usepercentage{<number>}
, the previous value is used, which may or may not be desired.
– egreg
yesterday
1
You can remove xfp and replace fpeval byfp_eval:n
.
– Ulrike Fischer
yesterday
|
show 3 more comments
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "85"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f468597%2fconvert-decimal-to-percentage-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the siunitx
package:
documentclass{article}
usepackage{siunitx}
newcommandpercentage[2][round-precision = 2]{% default precision: 2
SI[round-mode = places,
scientific-notation = fixed, fixed-exponent = 0,
output-decimal-marker={.}, #1]{#2e2}{percent}%
}
begin{document}
percentage{0.123456} % -> 12.35 %
percentage[round-precision = 3]{0.123456} % -> 12.345 %
end{document}
New contributor
add a comment |
You can use the siunitx
package:
documentclass{article}
usepackage{siunitx}
newcommandpercentage[2][round-precision = 2]{% default precision: 2
SI[round-mode = places,
scientific-notation = fixed, fixed-exponent = 0,
output-decimal-marker={.}, #1]{#2e2}{percent}%
}
begin{document}
percentage{0.123456} % -> 12.35 %
percentage[round-precision = 3]{0.123456} % -> 12.345 %
end{document}
New contributor
add a comment |
You can use the siunitx
package:
documentclass{article}
usepackage{siunitx}
newcommandpercentage[2][round-precision = 2]{% default precision: 2
SI[round-mode = places,
scientific-notation = fixed, fixed-exponent = 0,
output-decimal-marker={.}, #1]{#2e2}{percent}%
}
begin{document}
percentage{0.123456} % -> 12.35 %
percentage[round-precision = 3]{0.123456} % -> 12.345 %
end{document}
New contributor
You can use the siunitx
package:
documentclass{article}
usepackage{siunitx}
newcommandpercentage[2][round-precision = 2]{% default precision: 2
SI[round-mode = places,
scientific-notation = fixed, fixed-exponent = 0,
output-decimal-marker={.}, #1]{#2e2}{percent}%
}
begin{document}
percentage{0.123456} % -> 12.35 %
percentage[round-precision = 3]{0.123456} % -> 12.345 %
end{document}
New contributor
edited yesterday
New contributor
answered yesterday
zetaeffe
1515
1515
New contributor
New contributor
add a comment |
add a comment |
Your values looks wrong. But beside this:
documentclass{article}
usepackage{xfp,xparse}
ExplSyntaxOn
int_new:Nl_bjprim_round_int
keys_define:nn {bjprim}
{
positions-after-comma .int_set:N = l_bjprim_round_int
}
NewDocumentCommandpercentage { O{} m }
{
keys_set:nn {bjprim}{positions-after-comma=2,#1}
fpeval{round(#2*100,l_bjprim_round_int)}%
}
ExplSyntaxOff
begin{document}
percentage[positions-after-comma = 2]{0.123456}: 12.12%
percentage[positions-after-comma = 4]{0.123456}: 12.1235%
percentage[positions-after-comma = 4]{2.123456}: 212.1235%
percentage[positions-after-comma = 1]{0.6789}: 67.9%
percentage[positions-after-comma = 5]{0.6789}: 67.89000%
end{document}
If you really want to fill up with zeros you could round with siunitx:
documentclass{article}
usepackage{xfp,xparse,siunitx}
ExplSyntaxOn
int_new:Nl_bjprim_round_int
keys_define:nn {bjprim}
{
positions-after-comma .int_set:N = l_bjprim_round_int
}
NewDocumentCommandpercentage { O{} m }
{
keys_set:nn {bjprim}{positions-after-comma=2,#1}
num[round-mode = places,round-precision=l_bjprim_round_int]{fpeval{#2*100}}%
}
ExplSyntaxOff
begin{document}
percentage[positions-after-comma = 2]{0.123456}: 12.12%
percentage[positions-after-comma = 4]{0.123456}: 12.1235%
percentage[positions-after-comma = 4]{2.123456}: 212.1235%
percentage[positions-after-comma = 1]{0.6789}: 67.9%
percentage[positions-after-comma = 5]{0.6789}: 67.89000%
end{document}
This way the optional argument is actually mandatory.
– egreg
yesterday
@egreg thanks, I changed to O{}.
– Ulrike Fischer
yesterday
I am struggling to get your code running. It says: "File `xfp.sty' not found." I am working with Overleaf, can it be that this package is not supported?
– BJPrim
yesterday
That's not what I meant; if you usepercentage{<number>}
, the previous value is used, which may or may not be desired.
– egreg
yesterday
1
You can remove xfp and replace fpeval byfp_eval:n
.
– Ulrike Fischer
yesterday
|
show 3 more comments
Your values looks wrong. But beside this:
documentclass{article}
usepackage{xfp,xparse}
ExplSyntaxOn
int_new:Nl_bjprim_round_int
keys_define:nn {bjprim}
{
positions-after-comma .int_set:N = l_bjprim_round_int
}
NewDocumentCommandpercentage { O{} m }
{
keys_set:nn {bjprim}{positions-after-comma=2,#1}
fpeval{round(#2*100,l_bjprim_round_int)}%
}
ExplSyntaxOff
begin{document}
percentage[positions-after-comma = 2]{0.123456}: 12.12%
percentage[positions-after-comma = 4]{0.123456}: 12.1235%
percentage[positions-after-comma = 4]{2.123456}: 212.1235%
percentage[positions-after-comma = 1]{0.6789}: 67.9%
percentage[positions-after-comma = 5]{0.6789}: 67.89000%
end{document}
If you really want to fill up with zeros you could round with siunitx:
documentclass{article}
usepackage{xfp,xparse,siunitx}
ExplSyntaxOn
int_new:Nl_bjprim_round_int
keys_define:nn {bjprim}
{
positions-after-comma .int_set:N = l_bjprim_round_int
}
NewDocumentCommandpercentage { O{} m }
{
keys_set:nn {bjprim}{positions-after-comma=2,#1}
num[round-mode = places,round-precision=l_bjprim_round_int]{fpeval{#2*100}}%
}
ExplSyntaxOff
begin{document}
percentage[positions-after-comma = 2]{0.123456}: 12.12%
percentage[positions-after-comma = 4]{0.123456}: 12.1235%
percentage[positions-after-comma = 4]{2.123456}: 212.1235%
percentage[positions-after-comma = 1]{0.6789}: 67.9%
percentage[positions-after-comma = 5]{0.6789}: 67.89000%
end{document}
This way the optional argument is actually mandatory.
– egreg
yesterday
@egreg thanks, I changed to O{}.
– Ulrike Fischer
yesterday
I am struggling to get your code running. It says: "File `xfp.sty' not found." I am working with Overleaf, can it be that this package is not supported?
– BJPrim
yesterday
That's not what I meant; if you usepercentage{<number>}
, the previous value is used, which may or may not be desired.
– egreg
yesterday
1
You can remove xfp and replace fpeval byfp_eval:n
.
– Ulrike Fischer
yesterday
|
show 3 more comments
Your values looks wrong. But beside this:
documentclass{article}
usepackage{xfp,xparse}
ExplSyntaxOn
int_new:Nl_bjprim_round_int
keys_define:nn {bjprim}
{
positions-after-comma .int_set:N = l_bjprim_round_int
}
NewDocumentCommandpercentage { O{} m }
{
keys_set:nn {bjprim}{positions-after-comma=2,#1}
fpeval{round(#2*100,l_bjprim_round_int)}%
}
ExplSyntaxOff
begin{document}
percentage[positions-after-comma = 2]{0.123456}: 12.12%
percentage[positions-after-comma = 4]{0.123456}: 12.1235%
percentage[positions-after-comma = 4]{2.123456}: 212.1235%
percentage[positions-after-comma = 1]{0.6789}: 67.9%
percentage[positions-after-comma = 5]{0.6789}: 67.89000%
end{document}
If you really want to fill up with zeros you could round with siunitx:
documentclass{article}
usepackage{xfp,xparse,siunitx}
ExplSyntaxOn
int_new:Nl_bjprim_round_int
keys_define:nn {bjprim}
{
positions-after-comma .int_set:N = l_bjprim_round_int
}
NewDocumentCommandpercentage { O{} m }
{
keys_set:nn {bjprim}{positions-after-comma=2,#1}
num[round-mode = places,round-precision=l_bjprim_round_int]{fpeval{#2*100}}%
}
ExplSyntaxOff
begin{document}
percentage[positions-after-comma = 2]{0.123456}: 12.12%
percentage[positions-after-comma = 4]{0.123456}: 12.1235%
percentage[positions-after-comma = 4]{2.123456}: 212.1235%
percentage[positions-after-comma = 1]{0.6789}: 67.9%
percentage[positions-after-comma = 5]{0.6789}: 67.89000%
end{document}
Your values looks wrong. But beside this:
documentclass{article}
usepackage{xfp,xparse}
ExplSyntaxOn
int_new:Nl_bjprim_round_int
keys_define:nn {bjprim}
{
positions-after-comma .int_set:N = l_bjprim_round_int
}
NewDocumentCommandpercentage { O{} m }
{
keys_set:nn {bjprim}{positions-after-comma=2,#1}
fpeval{round(#2*100,l_bjprim_round_int)}%
}
ExplSyntaxOff
begin{document}
percentage[positions-after-comma = 2]{0.123456}: 12.12%
percentage[positions-after-comma = 4]{0.123456}: 12.1235%
percentage[positions-after-comma = 4]{2.123456}: 212.1235%
percentage[positions-after-comma = 1]{0.6789}: 67.9%
percentage[positions-after-comma = 5]{0.6789}: 67.89000%
end{document}
If you really want to fill up with zeros you could round with siunitx:
documentclass{article}
usepackage{xfp,xparse,siunitx}
ExplSyntaxOn
int_new:Nl_bjprim_round_int
keys_define:nn {bjprim}
{
positions-after-comma .int_set:N = l_bjprim_round_int
}
NewDocumentCommandpercentage { O{} m }
{
keys_set:nn {bjprim}{positions-after-comma=2,#1}
num[round-mode = places,round-precision=l_bjprim_round_int]{fpeval{#2*100}}%
}
ExplSyntaxOff
begin{document}
percentage[positions-after-comma = 2]{0.123456}: 12.12%
percentage[positions-after-comma = 4]{0.123456}: 12.1235%
percentage[positions-after-comma = 4]{2.123456}: 212.1235%
percentage[positions-after-comma = 1]{0.6789}: 67.9%
percentage[positions-after-comma = 5]{0.6789}: 67.89000%
end{document}
edited yesterday
answered yesterday
Ulrike Fischer
187k7291670
187k7291670
This way the optional argument is actually mandatory.
– egreg
yesterday
@egreg thanks, I changed to O{}.
– Ulrike Fischer
yesterday
I am struggling to get your code running. It says: "File `xfp.sty' not found." I am working with Overleaf, can it be that this package is not supported?
– BJPrim
yesterday
That's not what I meant; if you usepercentage{<number>}
, the previous value is used, which may or may not be desired.
– egreg
yesterday
1
You can remove xfp and replace fpeval byfp_eval:n
.
– Ulrike Fischer
yesterday
|
show 3 more comments
This way the optional argument is actually mandatory.
– egreg
yesterday
@egreg thanks, I changed to O{}.
– Ulrike Fischer
yesterday
I am struggling to get your code running. It says: "File `xfp.sty' not found." I am working with Overleaf, can it be that this package is not supported?
– BJPrim
yesterday
That's not what I meant; if you usepercentage{<number>}
, the previous value is used, which may or may not be desired.
– egreg
yesterday
1
You can remove xfp and replace fpeval byfp_eval:n
.
– Ulrike Fischer
yesterday
This way the optional argument is actually mandatory.
– egreg
yesterday
This way the optional argument is actually mandatory.
– egreg
yesterday
@egreg thanks, I changed to O{}.
– Ulrike Fischer
yesterday
@egreg thanks, I changed to O{}.
– Ulrike Fischer
yesterday
I am struggling to get your code running. It says: "File `xfp.sty' not found." I am working with Overleaf, can it be that this package is not supported?
– BJPrim
yesterday
I am struggling to get your code running. It says: "File `xfp.sty' not found." I am working with Overleaf, can it be that this package is not supported?
– BJPrim
yesterday
That's not what I meant; if you use
percentage{<number>}
, the previous value is used, which may or may not be desired.– egreg
yesterday
That's not what I meant; if you use
percentage{<number>}
, the previous value is used, which may or may not be desired.– egreg
yesterday
1
1
You can remove xfp and replace fpeval by
fp_eval:n
.– Ulrike Fischer
yesterday
You can remove xfp and replace fpeval by
fp_eval:n
.– Ulrike Fischer
yesterday
|
show 3 more comments
Thanks for contributing an answer to TeX - LaTeX Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f468597%2fconvert-decimal-to-percentage-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown