double.ToString truncates last 2 digits while I can still see them while debugging










1















How to I get double.ToString to not sometimes truncate last digits.



If I show a double in Visualstudio Immediate window or Quickwatch I see 17 decimals. When I convert it to a string I see 15.

There is probably a good reason for this but my problem is how to get around it.



? 0.87912328514094507
0.87912328514094507
? 0.87912328514094507.ToString()
"0,879123285140945"


Dotnet 4.6.2 on a Win7 fwiw.










share|improve this question



















  • 2





    0.87912328514094507.ToString("R") - round trip formatting - docs.microsoft.com/en-us/dotnet/standard/base-types/…

    – Dmitry Bychenko
    Nov 16 '18 at 6:54







  • 1





    Note that technically, both representations are truncating the underlying double. If your goal is to get an exact representation, you may want to consider using Jon Skeet's DoubleConverter class. See stackoverflow.com/a/1658420/18192 for discussion. Note that usually using the exact representation is inappropriate, since it displays more precision than a double can actually represent.

    – Brian
    Nov 16 '18 at 14:15















1















How to I get double.ToString to not sometimes truncate last digits.



If I show a double in Visualstudio Immediate window or Quickwatch I see 17 decimals. When I convert it to a string I see 15.

There is probably a good reason for this but my problem is how to get around it.



? 0.87912328514094507
0.87912328514094507
? 0.87912328514094507.ToString()
"0,879123285140945"


Dotnet 4.6.2 on a Win7 fwiw.










share|improve this question



















  • 2





    0.87912328514094507.ToString("R") - round trip formatting - docs.microsoft.com/en-us/dotnet/standard/base-types/…

    – Dmitry Bychenko
    Nov 16 '18 at 6:54







  • 1





    Note that technically, both representations are truncating the underlying double. If your goal is to get an exact representation, you may want to consider using Jon Skeet's DoubleConverter class. See stackoverflow.com/a/1658420/18192 for discussion. Note that usually using the exact representation is inappropriate, since it displays more precision than a double can actually represent.

    – Brian
    Nov 16 '18 at 14:15













1












1








1








How to I get double.ToString to not sometimes truncate last digits.



If I show a double in Visualstudio Immediate window or Quickwatch I see 17 decimals. When I convert it to a string I see 15.

There is probably a good reason for this but my problem is how to get around it.



? 0.87912328514094507
0.87912328514094507
? 0.87912328514094507.ToString()
"0,879123285140945"


Dotnet 4.6.2 on a Win7 fwiw.










share|improve this question
















How to I get double.ToString to not sometimes truncate last digits.



If I show a double in Visualstudio Immediate window or Quickwatch I see 17 decimals. When I convert it to a string I see 15.

There is probably a good reason for this but my problem is how to get around it.



? 0.87912328514094507
0.87912328514094507
? 0.87912328514094507.ToString()
"0,879123285140945"


Dotnet 4.6.2 on a Win7 fwiw.







c# .net






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 12:16







LosManos

















asked Nov 16 '18 at 6:53









LosManosLosManos

3,51452767




3,51452767







  • 2





    0.87912328514094507.ToString("R") - round trip formatting - docs.microsoft.com/en-us/dotnet/standard/base-types/…

    – Dmitry Bychenko
    Nov 16 '18 at 6:54







  • 1





    Note that technically, both representations are truncating the underlying double. If your goal is to get an exact representation, you may want to consider using Jon Skeet's DoubleConverter class. See stackoverflow.com/a/1658420/18192 for discussion. Note that usually using the exact representation is inappropriate, since it displays more precision than a double can actually represent.

    – Brian
    Nov 16 '18 at 14:15












  • 2





    0.87912328514094507.ToString("R") - round trip formatting - docs.microsoft.com/en-us/dotnet/standard/base-types/…

    – Dmitry Bychenko
    Nov 16 '18 at 6:54







  • 1





    Note that technically, both representations are truncating the underlying double. If your goal is to get an exact representation, you may want to consider using Jon Skeet's DoubleConverter class. See stackoverflow.com/a/1658420/18192 for discussion. Note that usually using the exact representation is inappropriate, since it displays more precision than a double can actually represent.

    – Brian
    Nov 16 '18 at 14:15







2




2





0.87912328514094507.ToString("R") - round trip formatting - docs.microsoft.com/en-us/dotnet/standard/base-types/…

– Dmitry Bychenko
Nov 16 '18 at 6:54






0.87912328514094507.ToString("R") - round trip formatting - docs.microsoft.com/en-us/dotnet/standard/base-types/…

– Dmitry Bychenko
Nov 16 '18 at 6:54





1




1





Note that technically, both representations are truncating the underlying double. If your goal is to get an exact representation, you may want to consider using Jon Skeet's DoubleConverter class. See stackoverflow.com/a/1658420/18192 for discussion. Note that usually using the exact representation is inappropriate, since it displays more precision than a double can actually represent.

– Brian
Nov 16 '18 at 14:15





Note that technically, both representations are truncating the underlying double. If your goal is to get an exact representation, you may want to consider using Jon Skeet's DoubleConverter class. See stackoverflow.com/a/1658420/18192 for discussion. Note that usually using the exact representation is inappropriate, since it displays more precision than a double can actually represent.

– Brian
Nov 16 '18 at 14:15












1 Answer
1






active

oldest

votes


















11














There are two standard mechanisms for achieving that.



If you pass "R" as the argument to ToString then you get the round trip string. That is, the string where converting the double to a string, then the string back to a double always gives you the double you started with.



If you pass "G17" as the argument then you get the standard string format but extended to 17 decimal places.



You can certainly be forgiven for not knowing that! This mechanism is not particularly discoverable. I'm not a big fan of how formatting strings have failed to evolve in the last 40 years.



For more details, read the documentation: https://docs.microsoft.com/en-us/dotnet/api/system.double.tostring






share|improve this answer


















  • 1





    It seems "R" does not always work for double. In docs I read "For Double values, the "R" format specifier in some cases fails to successfully round-trip the original value. For both Double and Single values, it also offers relatively poor performance. Instead, we recommend that you use the "G17" format specifier for Double values and the "G9" format specifier to successfully round-trip Single values."

    – LosManos
    Nov 16 '18 at 7:30












  • Future readers: According to my comment @ericlippert 's comment was incorrect. As I understand ToString("G17") should be the correct answer. Should I remove the tick marking this answer as correct?

    – LosManos
    Nov 16 '18 at 12:18











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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332834%2fdouble-tostring-truncates-last-2-digits-while-i-can-still-see-them-while-debuggi%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









11














There are two standard mechanisms for achieving that.



If you pass "R" as the argument to ToString then you get the round trip string. That is, the string where converting the double to a string, then the string back to a double always gives you the double you started with.



If you pass "G17" as the argument then you get the standard string format but extended to 17 decimal places.



You can certainly be forgiven for not knowing that! This mechanism is not particularly discoverable. I'm not a big fan of how formatting strings have failed to evolve in the last 40 years.



For more details, read the documentation: https://docs.microsoft.com/en-us/dotnet/api/system.double.tostring






share|improve this answer


















  • 1





    It seems "R" does not always work for double. In docs I read "For Double values, the "R" format specifier in some cases fails to successfully round-trip the original value. For both Double and Single values, it also offers relatively poor performance. Instead, we recommend that you use the "G17" format specifier for Double values and the "G9" format specifier to successfully round-trip Single values."

    – LosManos
    Nov 16 '18 at 7:30












  • Future readers: According to my comment @ericlippert 's comment was incorrect. As I understand ToString("G17") should be the correct answer. Should I remove the tick marking this answer as correct?

    – LosManos
    Nov 16 '18 at 12:18















11














There are two standard mechanisms for achieving that.



If you pass "R" as the argument to ToString then you get the round trip string. That is, the string where converting the double to a string, then the string back to a double always gives you the double you started with.



If you pass "G17" as the argument then you get the standard string format but extended to 17 decimal places.



You can certainly be forgiven for not knowing that! This mechanism is not particularly discoverable. I'm not a big fan of how formatting strings have failed to evolve in the last 40 years.



For more details, read the documentation: https://docs.microsoft.com/en-us/dotnet/api/system.double.tostring






share|improve this answer


















  • 1





    It seems "R" does not always work for double. In docs I read "For Double values, the "R" format specifier in some cases fails to successfully round-trip the original value. For both Double and Single values, it also offers relatively poor performance. Instead, we recommend that you use the "G17" format specifier for Double values and the "G9" format specifier to successfully round-trip Single values."

    – LosManos
    Nov 16 '18 at 7:30












  • Future readers: According to my comment @ericlippert 's comment was incorrect. As I understand ToString("G17") should be the correct answer. Should I remove the tick marking this answer as correct?

    – LosManos
    Nov 16 '18 at 12:18













11












11








11







There are two standard mechanisms for achieving that.



If you pass "R" as the argument to ToString then you get the round trip string. That is, the string where converting the double to a string, then the string back to a double always gives you the double you started with.



If you pass "G17" as the argument then you get the standard string format but extended to 17 decimal places.



You can certainly be forgiven for not knowing that! This mechanism is not particularly discoverable. I'm not a big fan of how formatting strings have failed to evolve in the last 40 years.



For more details, read the documentation: https://docs.microsoft.com/en-us/dotnet/api/system.double.tostring






share|improve this answer













There are two standard mechanisms for achieving that.



If you pass "R" as the argument to ToString then you get the round trip string. That is, the string where converting the double to a string, then the string back to a double always gives you the double you started with.



If you pass "G17" as the argument then you get the standard string format but extended to 17 decimal places.



You can certainly be forgiven for not knowing that! This mechanism is not particularly discoverable. I'm not a big fan of how formatting strings have failed to evolve in the last 40 years.



For more details, read the documentation: https://docs.microsoft.com/en-us/dotnet/api/system.double.tostring







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 6:58









Eric LippertEric Lippert

547k14610681953




547k14610681953







  • 1





    It seems "R" does not always work for double. In docs I read "For Double values, the "R" format specifier in some cases fails to successfully round-trip the original value. For both Double and Single values, it also offers relatively poor performance. Instead, we recommend that you use the "G17" format specifier for Double values and the "G9" format specifier to successfully round-trip Single values."

    – LosManos
    Nov 16 '18 at 7:30












  • Future readers: According to my comment @ericlippert 's comment was incorrect. As I understand ToString("G17") should be the correct answer. Should I remove the tick marking this answer as correct?

    – LosManos
    Nov 16 '18 at 12:18












  • 1





    It seems "R" does not always work for double. In docs I read "For Double values, the "R" format specifier in some cases fails to successfully round-trip the original value. For both Double and Single values, it also offers relatively poor performance. Instead, we recommend that you use the "G17" format specifier for Double values and the "G9" format specifier to successfully round-trip Single values."

    – LosManos
    Nov 16 '18 at 7:30












  • Future readers: According to my comment @ericlippert 's comment was incorrect. As I understand ToString("G17") should be the correct answer. Should I remove the tick marking this answer as correct?

    – LosManos
    Nov 16 '18 at 12:18







1




1





It seems "R" does not always work for double. In docs I read "For Double values, the "R" format specifier in some cases fails to successfully round-trip the original value. For both Double and Single values, it also offers relatively poor performance. Instead, we recommend that you use the "G17" format specifier for Double values and the "G9" format specifier to successfully round-trip Single values."

– LosManos
Nov 16 '18 at 7:30






It seems "R" does not always work for double. In docs I read "For Double values, the "R" format specifier in some cases fails to successfully round-trip the original value. For both Double and Single values, it also offers relatively poor performance. Instead, we recommend that you use the "G17" format specifier for Double values and the "G9" format specifier to successfully round-trip Single values."

– LosManos
Nov 16 '18 at 7:30














Future readers: According to my comment @ericlippert 's comment was incorrect. As I understand ToString("G17") should be the correct answer. Should I remove the tick marking this answer as correct?

– LosManos
Nov 16 '18 at 12:18





Future readers: According to my comment @ericlippert 's comment was incorrect. As I understand ToString("G17") should be the correct answer. Should I remove the tick marking this answer as correct?

– LosManos
Nov 16 '18 at 12:18



















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53332834%2fdouble-tostring-truncates-last-2-digits-while-i-can-still-see-them-while-debuggi%23new-answer', 'question_page');

);

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







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線