Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Float Precision appears to be 9 digits?

Tags:

c#

precision

I'm porting an app from .NET to the Mono Runtime, and at one point in the code I can see that a float has the value 158136.422. My understanding of float was that it was 7 digit precision, so how is that number acceptable? Mono trims it to 7 digits as I'd expect.

If in C# I do

float f = 158136.422f;

It gets rounded to 158136.4.

like image 449
user2617833 Avatar asked Feb 04 '26 17:02

user2617833


1 Answers

When you write this code:

float f = 158136.422f;

The exact value assigned to f is 158136.421875.

How many digits are output in a string representation is a different matter.

When I use:

float f = 158136.422f;
Console.WriteLine(f);
Console.WriteLine(f.ToString("r"));

I get results of:

158136.4
158136.422

on both Mono and .NET.

like image 139
Jon Skeet Avatar answered Feb 06 '26 06:02

Jon Skeet