PC Review


Reply
Thread Tools Rate Thread

CultureInfo problem

 
 
=?Utf-8?B?d2luZHNpbQ==?=
Guest
Posts: n/a
 
      26th Feb 2007
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
 
Reply With Quote
 
 
 
 
=?UTF-8?B?R8O2cmFuIEFuZGVyc3Nvbg==?=
Guest
Posts: n/a
 
      26th Feb 2007
windsim wrote:
> 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.

--
Göran Andersson
_____
http://www.guffa.com
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      26th Feb 2007
windsim <(E-Mail Removed)> wrote:
> 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

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
=?Utf-8?B?d2luZHNpbQ==?=
Guest
Posts: n/a
 
      26th Feb 2007
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


"Jon Skeet [C# MVP]" wrote:

> windsim <(E-Mail Removed)> wrote:
> > 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
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
>

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      26th Feb 2007
windsim <(E-Mail Removed)> wrote:
> 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?

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
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
CultureInfo problem Tom Microsoft Dot NET Framework 1 26th Jul 2007 09:44 AM
Need Help: DateTime.Parse CultureInfo Problem kilaen@gmail.com Microsoft Dot NET Framework 1 6th Apr 2007 12:19 AM
Problem CultureInfo Giorgio Microsoft VB .NET 2 2nd Aug 2005 04:46 PM
Problem with CultureInfo =?Utf-8?B?dG9kb3Jvdi1ma3Q=?= Microsoft C# .NET 0 2nd Oct 2004 05:39 PM
Serializing CultureInfo problem =?Utf-8?B?Y2hveWsx?= Microsoft C# .NET 2 8th Jul 2004 05:04 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:58 AM.