Pretty simple situation, but I got stuck exactly here. So, I have next method:
public static float? ToSingleNullable(string value)
{
if (string.IsNullOrEmpty(value))
return null;
else
{
float number = 0;
if (float.TryParse(value, out number))
return number;
return null;
}
}
In a situation where something more than 19 999 999 comes in, then float.TryParse
instead of, say, 20 000 012 outputs 2e+6 or something like that. In case of reverse conversion, at best, I get 20 000 010. And I can't predict what will happen - 20 000 000 or 20 000 010.
In general, how to make the current method return a floating point number exactly in decimal notation?
There are a lot of examples on the web for .ToString()
, but I couldn't find anything about the reverse conversion. Changing to decimal/double
won't work - the method must return exactly float, because we have 100500+ lines of code, which're using nullable float
.
The string is entered by the user in "human form". So, we're getting omething like this:
string str = "20100999";
float number = ToSingleNullable(str);
Console.WriteLine(number);//about 2е+6. When convert to string - 20 000 000
When declare variables I can put next code:
float a = 20 100 100, b = 20 100 100f;
So, first variable will transform into exponential form, but second will stay unchanged. I need something like that for my method.
Maybe there's the way to parse string
to double/decimal
and then convert to float?
, but all the ways I've tried didn't worked. Still, method always returns exponential notation, what unacceptable.