Need to restrict decimal to 20
I am having a scenario where I convert number into user preference string
conv = num.toLocaleString('de-DE', { maximumFractionDigits: 20 })
As I have real long numbers I want to restrict upto 20 decimal points.
But above code is not giving me desired output.
Can anyone please suggest how to achieve same?
Thanks,
Nupur
javascript locale
add a comment |
I am having a scenario where I convert number into user preference string
conv = num.toLocaleString('de-DE', { maximumFractionDigits: 20 })
As I have real long numbers I want to restrict upto 20 decimal points.
But above code is not giving me desired output.
Can anyone please suggest how to achieve same?
Thanks,
Nupur
javascript locale
What is num? Perhaps it is a string?
– HerrSerker
Nov 13 '18 at 9:41
The questions should be asking what browser are you using because according to this that could be your problem.
– Adriani6
Nov 13 '18 at 9:46
You don't say what your problem is.(2/3).toLocaleString("de-DE", { maximumFractionDigits: 20 })
prints0,6666666666666666
in Firefox so I can speculate you're expecting to squeeze more precision that JavaScript is able to provide.
– Álvaro González
Nov 13 '18 at 9:48
You should at least provide an example of the number you're putting in, and the output you're expecting. Since double precision float numbers only have 15-17 significant decimal digits, using a value of 20 won't do you any good.
– Robby Cornelissen
Nov 13 '18 at 9:48
add a comment |
I am having a scenario where I convert number into user preference string
conv = num.toLocaleString('de-DE', { maximumFractionDigits: 20 })
As I have real long numbers I want to restrict upto 20 decimal points.
But above code is not giving me desired output.
Can anyone please suggest how to achieve same?
Thanks,
Nupur
javascript locale
I am having a scenario where I convert number into user preference string
conv = num.toLocaleString('de-DE', { maximumFractionDigits: 20 })
As I have real long numbers I want to restrict upto 20 decimal points.
But above code is not giving me desired output.
Can anyone please suggest how to achieve same?
Thanks,
Nupur
javascript locale
javascript locale
edited Nov 13 '18 at 9:40
Federico klez Culloca
15.6k134275
15.6k134275
asked Nov 13 '18 at 9:39
NupurNupur
36249
36249
What is num? Perhaps it is a string?
– HerrSerker
Nov 13 '18 at 9:41
The questions should be asking what browser are you using because according to this that could be your problem.
– Adriani6
Nov 13 '18 at 9:46
You don't say what your problem is.(2/3).toLocaleString("de-DE", { maximumFractionDigits: 20 })
prints0,6666666666666666
in Firefox so I can speculate you're expecting to squeeze more precision that JavaScript is able to provide.
– Álvaro González
Nov 13 '18 at 9:48
You should at least provide an example of the number you're putting in, and the output you're expecting. Since double precision float numbers only have 15-17 significant decimal digits, using a value of 20 won't do you any good.
– Robby Cornelissen
Nov 13 '18 at 9:48
add a comment |
What is num? Perhaps it is a string?
– HerrSerker
Nov 13 '18 at 9:41
The questions should be asking what browser are you using because according to this that could be your problem.
– Adriani6
Nov 13 '18 at 9:46
You don't say what your problem is.(2/3).toLocaleString("de-DE", { maximumFractionDigits: 20 })
prints0,6666666666666666
in Firefox so I can speculate you're expecting to squeeze more precision that JavaScript is able to provide.
– Álvaro González
Nov 13 '18 at 9:48
You should at least provide an example of the number you're putting in, and the output you're expecting. Since double precision float numbers only have 15-17 significant decimal digits, using a value of 20 won't do you any good.
– Robby Cornelissen
Nov 13 '18 at 9:48
What is num? Perhaps it is a string?
– HerrSerker
Nov 13 '18 at 9:41
What is num? Perhaps it is a string?
– HerrSerker
Nov 13 '18 at 9:41
The questions should be asking what browser are you using because according to this that could be your problem.
– Adriani6
Nov 13 '18 at 9:46
The questions should be asking what browser are you using because according to this that could be your problem.
– Adriani6
Nov 13 '18 at 9:46
You don't say what your problem is.
(2/3).toLocaleString("de-DE", { maximumFractionDigits: 20 })
prints 0,6666666666666666
in Firefox so I can speculate you're expecting to squeeze more precision that JavaScript is able to provide.– Álvaro González
Nov 13 '18 at 9:48
You don't say what your problem is.
(2/3).toLocaleString("de-DE", { maximumFractionDigits: 20 })
prints 0,6666666666666666
in Firefox so I can speculate you're expecting to squeeze more precision that JavaScript is able to provide.– Álvaro González
Nov 13 '18 at 9:48
You should at least provide an example of the number you're putting in, and the output you're expecting. Since double precision float numbers only have 15-17 significant decimal digits, using a value of 20 won't do you any good.
– Robby Cornelissen
Nov 13 '18 at 9:48
You should at least provide an example of the number you're putting in, and the output you're expecting. Since double precision float numbers only have 15-17 significant decimal digits, using a value of 20 won't do you any good.
– Robby Cornelissen
Nov 13 '18 at 9:48
add a comment |
1 Answer
1
active
oldest
votes
The problem lies with the way that floating point numbers are stored in the processor. See Floating Point arithmetic and Double-precision floating-point format.
You cannot have 20 significant decimal places in a number. They will get truncated at the end
See an example:num = 0.0000000000000000123456;
will show as "0,00000000000000001235"
andnum = 1.0000000000000000123456;
will show as "1,00000000000000000000"
If you want to force exactly 20 fractional digits use this options object:
{minimumFractionDigits: 20, maximumFractionDigits: 20}
1
The important part: you can force 20 digits but the last ones will be incorrect ((2/3).toLocaleString("de-DE", {minimumFractionDigits: 20, maximumFractionDigits: 20})
prints"0,66666666666666660000"
).
– Álvaro González
Nov 13 '18 at 14:01
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53277989%2fneed-to-restrict-decimal-to-20%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem lies with the way that floating point numbers are stored in the processor. See Floating Point arithmetic and Double-precision floating-point format.
You cannot have 20 significant decimal places in a number. They will get truncated at the end
See an example:num = 0.0000000000000000123456;
will show as "0,00000000000000001235"
andnum = 1.0000000000000000123456;
will show as "1,00000000000000000000"
If you want to force exactly 20 fractional digits use this options object:
{minimumFractionDigits: 20, maximumFractionDigits: 20}
1
The important part: you can force 20 digits but the last ones will be incorrect ((2/3).toLocaleString("de-DE", {minimumFractionDigits: 20, maximumFractionDigits: 20})
prints"0,66666666666666660000"
).
– Álvaro González
Nov 13 '18 at 14:01
add a comment |
The problem lies with the way that floating point numbers are stored in the processor. See Floating Point arithmetic and Double-precision floating-point format.
You cannot have 20 significant decimal places in a number. They will get truncated at the end
See an example:num = 0.0000000000000000123456;
will show as "0,00000000000000001235"
andnum = 1.0000000000000000123456;
will show as "1,00000000000000000000"
If you want to force exactly 20 fractional digits use this options object:
{minimumFractionDigits: 20, maximumFractionDigits: 20}
1
The important part: you can force 20 digits but the last ones will be incorrect ((2/3).toLocaleString("de-DE", {minimumFractionDigits: 20, maximumFractionDigits: 20})
prints"0,66666666666666660000"
).
– Álvaro González
Nov 13 '18 at 14:01
add a comment |
The problem lies with the way that floating point numbers are stored in the processor. See Floating Point arithmetic and Double-precision floating-point format.
You cannot have 20 significant decimal places in a number. They will get truncated at the end
See an example:num = 0.0000000000000000123456;
will show as "0,00000000000000001235"
andnum = 1.0000000000000000123456;
will show as "1,00000000000000000000"
If you want to force exactly 20 fractional digits use this options object:
{minimumFractionDigits: 20, maximumFractionDigits: 20}
The problem lies with the way that floating point numbers are stored in the processor. See Floating Point arithmetic and Double-precision floating-point format.
You cannot have 20 significant decimal places in a number. They will get truncated at the end
See an example:num = 0.0000000000000000123456;
will show as "0,00000000000000001235"
andnum = 1.0000000000000000123456;
will show as "1,00000000000000000000"
If you want to force exactly 20 fractional digits use this options object:
{minimumFractionDigits: 20, maximumFractionDigits: 20}
answered Nov 13 '18 at 9:49
HerrSerkerHerrSerker
20k84779
20k84779
1
The important part: you can force 20 digits but the last ones will be incorrect ((2/3).toLocaleString("de-DE", {minimumFractionDigits: 20, maximumFractionDigits: 20})
prints"0,66666666666666660000"
).
– Álvaro González
Nov 13 '18 at 14:01
add a comment |
1
The important part: you can force 20 digits but the last ones will be incorrect ((2/3).toLocaleString("de-DE", {minimumFractionDigits: 20, maximumFractionDigits: 20})
prints"0,66666666666666660000"
).
– Álvaro González
Nov 13 '18 at 14:01
1
1
The important part: you can force 20 digits but the last ones will be incorrect (
(2/3).toLocaleString("de-DE", {minimumFractionDigits: 20, maximumFractionDigits: 20})
prints "0,66666666666666660000"
).– Álvaro González
Nov 13 '18 at 14:01
The important part: you can force 20 digits but the last ones will be incorrect (
(2/3).toLocaleString("de-DE", {minimumFractionDigits: 20, maximumFractionDigits: 20})
prints "0,66666666666666660000"
).– Álvaro González
Nov 13 '18 at 14:01
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53277989%2fneed-to-restrict-decimal-to-20%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
What is num? Perhaps it is a string?
– HerrSerker
Nov 13 '18 at 9:41
The questions should be asking what browser are you using because according to this that could be your problem.
– Adriani6
Nov 13 '18 at 9:46
You don't say what your problem is.
(2/3).toLocaleString("de-DE", { maximumFractionDigits: 20 })
prints0,6666666666666666
in Firefox so I can speculate you're expecting to squeeze more precision that JavaScript is able to provide.– Álvaro González
Nov 13 '18 at 9:48
You should at least provide an example of the number you're putting in, and the output you're expecting. Since double precision float numbers only have 15-17 significant decimal digits, using a value of 20 won't do you any good.
– Robby Cornelissen
Nov 13 '18 at 9:48