Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8724467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:50:14+00:00 2026-06-13T07:50:14+00:00

The following C++ program expectedly outputs 0.300000000000000040000 : cout << setprecision(40) << (0.1 +

  • 0

The following C++ program expectedly outputs 0.300000000000000040000:

cout << setprecision(40) << (0.1 + 0.2) << endl;

However the following C# code:

System.Console.WriteLine("{0:F40}", 0.1+0.2);

… unexpetedly outputs 0.3000000000000000000000000000000000000000, while it (expectedly) evaluates 0.1+0.2 == 0.3 as False.

Where is the ‘4’ and how can I output it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:50:15+00:00Added an answer on June 13, 2026 at 7:50 am

    What do you get if you say

    System.Console.WriteLine("{0:R}", 0.1+0.2);
    

    A format string like "F40" will only add zeros to the end of the string. It will not give you extra precision compared to just "G". On the other hand, "R", will reveal two extra digits (or one if the last of the two digits extra is 0) if (and only if) this is necessary to distinguish the number from a number closer to the string from "G".

    Addition: To get a better understanding of how the format strings work in .NET, try running the following code:

    double a = BitConverter.ToDouble(new byte[] { 49, 51, 51, 51, 51, 51, 211, 63, }, 0);
    double b = BitConverter.ToDouble(new byte[] { 50, 51, 51, 51, 51, 51, 211, 63, }, 0);
    double c = BitConverter.ToDouble(new byte[] { 51, 51, 51, 51, 51, 51, 211, 63, }, 0);
    double d = BitConverter.ToDouble(new byte[] { 52, 51, 51, 51, 51, 51, 211, 63, }, 0);
    double e = BitConverter.ToDouble(new byte[] { 53, 51, 51, 51, 51, 51, 211, 63, }, 0);
    
    Console.WriteLine("using G:");
    Console.WriteLine(a.ToString());
    Console.WriteLine(b.ToString());
    Console.WriteLine(c.ToString());
    Console.WriteLine(d.ToString());
    Console.WriteLine(e.ToString());
    Console.WriteLine("using F40:");
    Console.WriteLine(a.ToString("F40"));
    Console.WriteLine(b.ToString("F40"));
    Console.WriteLine(c.ToString("F40"));
    Console.WriteLine(d.ToString("F40"));
    Console.WriteLine(e.ToString("F40"));
    Console.WriteLine("using R:");
    Console.WriteLine(a.ToString("R"));
    Console.WriteLine(b.ToString("R"));
    Console.WriteLine(c.ToString("R"));
    Console.WriteLine(d.ToString("R"));
    Console.WriteLine(e.ToString("R"));
    

    Here, a, b, c, d, and e are “neighbors” as System.Doubles which is evident from the byte arrays. You should see that the "R" format string gives extra digits only in case of a, b, and d, e. For c, no extra digits are needed, but it is clear that if one were to write c with 17 figures, the last two of the 17 wouldn’t be 00 (see also Jon Skeet’s answer for the full decimal expansions which the methods built into .NET won’t give you easily).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following code: using System.Collections.Generic; static class Program { static void Main() {
Output of the following program is: First: System.String. Second: System.String. The expected result is:
I have some troubles with Haskell's type system. Situation: Following program is taking list
I have the following code. In windows server 2008, the program is correct and
Consider this simple console application: using System; namespace Demo { class Program { static
The following C++ program compiles and runs as expected: #include <stdio.h> int main(int argc,
I would have expected the following C# program to only print EOF! once I
The following program expects user input in the mixed fraction format 'whole_numbernumerator/denominator' and assigns
The following program I'm creating will allow the user will input values to create
The following program, compiled with g++ 4.6, yields the error request for member ‘y’

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.