0

I am integrating a library with my c# code. There is a decimal parameter i am using from the library. Parameter return usual decimal values mostly like: 0.24756400. But sometimes it returns values in Exponential format like: 0E-8.

When i try process the parameter in my code, for example when i parse it like this:

Decimal.Parse(paymentAuth.IyziCommissionFee);)

it gives me exception

Object reference not set to an instance of an object) when the value is in Exponential format.

If i can identify the value format (number or Exponential number) i can manage it.

So how can i identify if the value is in Exponential format? Alternatively, any suggestion to manage such situation?

Alex K.
  • 159,548
  • 29
  • 245
  • 267
P. Ramos
  • 1
  • 1
  • 1
    This seems not right. This exception is probably thrown when `paymentAuth` is `null`, not depending of the format. – René Vogt Jul 19 '16 at 16:04
  • The indicated error is not a parse error, you need to provide more details. – Alex K. Jul 19 '16 at 16:04
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – René Vogt Jul 19 '16 at 16:05

1 Answers1

1

You don't need to identify if it is an exponent or not

If you want to parse a string that could be an exponential format into a decimal you can use the overload of the Decimal.Parse method that takes a System.Globalization.NumberStyles parameter to do this.

var result = Decimal.Parse(
              paymentAuth.IyziCommissionFee,
              System.Globalization.NumberStyles.Float
            );

This specifies that the incoming string is a float, which you can represent using the exponential format you're talking about.

You can read the MSDN docs about this overload and the NumberStyles type here :

konkked
  • 3,053
  • 12
  • 19