Float size and display question

Emmad Kareem 81 Reputation points
2020-12-26T20:58:37.187+00:00

In the code below, I have 2 questions:
1-How to show the full value stored in variable c?
2-The documentaion states that float can hold 6 to 9 digits only, yet its holding a larger value (if result is true) - How is this possible?

float c = 9223372036854775807;
if ( 9223372036854775807==c ){
Console.WriteLine("{0}",c); // c = 9.223372E+18 Why c shows this value in display?
Console.WriteLine("{0:E15}",c); // Even worse...9.223372040000000E+018
}

Thanks.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,355 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 112.9K Reputation points
    2020-12-26T21:37:53.047+00:00

    If you write ‘if( 9223372000000000000 == c )’ or ‘if( 9223372129999999999 == c )’, it will work too. Therefore, it holds between 6 and 9 digits, according to documentation.

    Try "{0:F}" and "{0:N}" specifiers too.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful