What is the best solution to be able to use both decimalcomma and decimalpoint in numbers

T

Tony Johansson

I live in Sweden and we use decimalcomma for float, double and decimal
numbers but some
people might use decimalpoint so I just wonder what is the best solution to
support both in calculation.
The code below works fine for decimalcomma but get formatException saying
that input string had wrong format.

One solution is to replace decimalpoint with decimalcomma but I don't know
if that is the best solution

protected void Operation_Click(object sender, EventArgs e)
{
switch (((Button)sender).Text)
{
case "+" : txtResult.Text = string.Format("{0:F3}",
Convert.ToDouble(txtNumber1.Text) + Convert.ToDouble(txtNumber2.Text));
break;
case "-": txtResult.Text = string.Format("{0:F3}",
Convert.ToDouble(txtNumber1.Text) - Convert.ToDouble(txtNumber2.Text));
break;
case "*": txtResult.Text = string.Format("{0:F3}",
Convert.ToDouble(txtNumber1.Text) * Convert.ToDouble(txtNumber2.Text));
break;
case "/": txtResult.Text = string.Format("{0:F3}",
Convert.ToDouble(txtNumber1.Text) / Convert.ToDouble(txtNumber2.Text));
break;
}
}

//tony
 
U

Ulrik Magnusson

It seems that you can only have one "NumberDecimalSeparator" at a
time, so you need two calls to TryParse (there must be a better way?):

private static bool TryParse(string str, out double d)
{
// leave the CurrentCulture in its original state
CultureInfo culture = CultureInfo.CurrentCulture.Clone()
as CultureInfo;

foreach (string separator in new string[]{ ",", "."})
{
culture.NumberFormat.NumberDecimalSeparator =
separator;
if (Double.TryParse(str,
NumberStyles.AllowDecimalPoint, culture, out d))
return true;
}

d = 0;
return false;
}
 
T

Tony Johansson

I used a very simple soilution I replaced a decimalcomma with decimalpunkt
for my textField and it works fine.

//Tony
 
A

Arne Vajhøj

I live in Sweden and we use decimalcomma for float, double and decimal
numbers but some
people might use decimalpoint so I just wonder what is the best solution to
support both in calculation.
The code below works fine for decimalcomma but get formatException saying
that input string had wrong format.

One solution is to replace decimalpoint with decimalcomma but I don't know
if that is the best solution

You can do that. Or you can try both.

But I really believe that the best solution is to inform the user
about the expected format and give him an error for invalid formats.

Arne
 
U

Ulrik Magnusson

Nice and simple ;-)
In will work until you install the program on a PC where the OS have
another NumberDecimalSeparator setting than on your developer-PC, and
you still have introduced a new problem of the number is including
thousand-separator as well.

Simple but not nice ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top