-1

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.

  • This is a very common misunderstanding. Float is a float, but when you ask Console.WriteLine to write that float to your screen, it needs to convert it to a string and then it changes to whatever format it wants for display. Some possible customization can be achieved using the ["Standard Numeric Format Strings"](https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings) but then remember that [float is not a precise number](https://stackoverflow.com/questions/2100490/floating-point-inaccuracy-examples) – Steve Aug 16 '20 at 13:47
  • @Steve, when I put a breakpoint after TryParse, I see that variable itself contains 2e+6, but not decimal value. Problem not with displaying/ToString(), but with parsing itself – Vlad Kravchenko Aug 16 '20 at 13:49
  • Again you are looking at that value using a tool that need to display the value on screen and it choose to use that format. – Steve Aug 16 '20 at 13:51
  • @AleksKeller I would say this can be a problem of float [precision](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/floating-point-numeric-types), not a problem of parsing. – Guru Stron Aug 16 '20 at 13:53
  • @GuruStron, when I'm writing value to a file using simple File.WriteAllText(number.ToString("F")); I'm still getting wrong, rounded value. I understand, there's no any possible way to fix this – Vlad Kravchenko Aug 16 '20 at 13:57
  • 1
    When you put a breakpoint after `TryParse` and ask the debugger to show you the value, the debugger converts the value to a string and shows you the string. The debugger can only show you characters; it cannot show you the abstract mathematical object that is a number. We represent numbers in various ways, such as decimal like “30” or “3e1” or “thirty.” These are all sequences of characters that represent the same thing. The debugger shows you one of them. That does not mean the number in `number` is in that form. – Eric Postpischil Aug 16 '20 at 13:57

1 Answers1

0

I don't really understand your problem. If you real search for numeric format strings you need to find this: Standard numeric format strings.

If you want to see a float number in decimal format use the .ToString("F") and you will get it without exponents. It's simple.

z-eh
  • 26
  • 2
  • When I put a breakpoint after TryParse, I see that variable itself contains 2e+6, but not decimal value. Problem not with displaying/ToString(), but with parsing itself – Vlad Kravchenko Aug 16 '20 at 13:49
  • 1
    If you use a debugger in Visual Studio 2019, or older version you have a Watch tab in the IDE. You can write after the variable the .ToString("F") to watch the variable non exponent representation. The debugger in default use the exponent representatation. Your number is fine. It contains the number, it just displays you in a form which not enough for you.. – z-eh Aug 16 '20 at 14:18