get FormatException

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hello!

I have a win form program implementing a calculator.
In this program a have button with the text decimalpoint which all
calculator have.

I have one problem here.
When I do the following I run into FormatException
double d = double.Parse(txtDisplay);
because txtDisplay = "0.2". as you can see we have a decimalpoint here
if I insted use a comma like , it works.

How do I fix to be able to use double.Parse("0.2") with decimalpoint

//Tony
 
Hello!

If I have the following
double d = 0.2;
and do
string s = d.ToString();
I get s = 0,2

Now as you can see ToString replaces
decimalpunkt(.) to comma(,)
but I want to keep decimalpunkt

How can I do to keep my decimalpunkt when using ToString on for example 0.2

//Tony

Peter Duniho said:
[...]
When I do the following I run into FormatException
double d = double.Parse(txtDisplay);
because txtDisplay = "0.2". as you can see we have a decimalpoint here
if I insted use a comma like , it works.

How do I fix to be able to use double.Parse("0.2") with decimalpoint

If your system is set to use a comma as the decimal separator, I would
think it would make more sense for you to use a comma in your number.

But if you insist on always using a period as the decimal separator, you
can use the Parse() overload that allows you to pass an IFormatProvider,
and pass the CultureInfo.InvariantCulture property value for that
argument.

Pete
 
Back
Top