Is it safe to cast Math.Round result to float?

Viewed 373

A colleague has written some code along these lines:

var roundedNumber = (float) Math.Round(someFloat, 2);
Console.WriteLine(roundedNumber);

I have an uncertainty about this code - is the number that gets written here even guaranteed to have 2 decimal places any more? It seems plausible to me that truncation of the double Math.Round(someFloat, 2) to float might result in a number whose string representation has more than 2 digits. Can anybody either provide an example of this (demonstrating that such a cast is unsafe) or else demonstrate somehow that it is safe to perform such a cast?

1 Answers
Related