CultureInfo problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I have a c# program where I need a numerical float input. Due to the
culture difference, someone is used to type f.ex. 1,3; 2,32 (, comma) while
others use 1.3; 2.32(. point) etc. so the program should accept both
numerical expression.
How can I do it?
 
windsim said:
Hi,
I have a c# program where I need a numerical float input. Due to the
culture difference, someone is used to type f.ex. 1,3; 2,32 (, comma) while
others use 1.3; 2.32(. point) etc. so the program should accept both
numerical expression.
How can I do it?

Replace commas with periods in the string, and use CultureInfo.Invariant
when you parse it.
 
windsim said:
I have a c# program where I need a numerical float input. Due to the
culture difference, someone is used to type f.ex. 1,3; 2,32 (, comma) while
others use 1.3; 2.32(. point) etc. so the program should accept both
numerical expression.
How can I do it?

Well, if you know the appropriate CultureInfo to use, pass it to
double.TryParse as the format provider. If you don't, could you just
replace all commas with points first? Note that that would cause a
problem if people start putting commas in for thousands, eg
1,234.45
 
Hi,
A universal solution is the best. I try to use:
Decimal data = Decimal.Parse((string)item.val, new
System.Globalization.CultureInfo("en-US") );
but my variable--item.val is object type, if I convert to string it will
lead to error.
does anyone know how to solve it?
 
windsim said:
A universal solution is the best. I try to use:
Decimal data = Decimal.Parse((string)item.val, new
System.Globalization.CultureInfo("en-US") );
but my variable--item.val is object type, if I convert to string it will
lead to error.
does anyone know how to solve it?

Well, first you need to work out what to parse. Ignore the parsing part
to start with - what *exactly* is item.val?
 

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

Back
Top