PC Review


Reply
Thread Tools Rate Thread

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

 
 
Tony Johansson
Guest
Posts: n/a
 
      11th May 2011
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


 
Reply With Quote
 
 
 
 
Ulrik Magnusson
Guest
Posts: n/a
 
      11th May 2011
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;
}
 
Reply With Quote
 
Tony Johansson
Guest
Posts: n/a
 
      11th May 2011
I used a very simple soilution I replaced a decimalcomma with decimalpunkt
for my textField and it works fine.

//Tony

"Ulrik Magnusson" <(E-Mail Removed)> skrev i meddelandet
news:eaeef480-44ae-4f80-938f-(E-Mail Removed)...
> 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;
> }



 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      12th May 2011
On 11-05-2011 14:26, Tony Johansson wrote:
> 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

 
Reply With Quote
 
Ulrik Magnusson
Guest
Posts: n/a
 
      12th May 2011
On May 12, 12:38*am, "Tony Johansson" <johansson.anders...@telia.com>
wrote:
> I used a very simple soilution I replaced a decimalcomma with decimalpunkt
> for my textField and it works fine.


Nice and simple ;-)
 
Reply With Quote
 
Ulrik Magnusson
Guest
Posts: n/a
 
      12th May 2011
> > 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 ;-)
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
General solution for missing sequence numbers Peter Danes Microsoft Access 33 8th Dec 2005 03:14 PM
General solution for missing sequence numbers Peter Danes Microsoft Access Form Coding 33 8th Dec 2005 03:14 PM
General solution for missing sequence numbers Peter Danes Microsoft Access Queries 33 8th Dec 2005 03:14 PM
how do I use my solver solution on next month's numbers =?Utf-8?B?SmltIE11bGxpbg==?= Microsoft Excel Programming 0 4th Oct 2005 01:20 AM
rebuild solution automaticly each time im running the solution in the ide Daylor Microsoft Dot NET 1 24th Sep 2003 05:33 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:20 AM.