PC Review


Reply
Thread Tools Rate Thread

Converting string numbers to numbers

 
 
Anders Eriksson
Guest
Posts: n/a
 
      25th Jan 2011
Hello,

I'm reading a text file that has some numbers in it (both integer and
double). I then want to convert the string number into an integer or double.

Since I'm in Sweden, .NET assumes that I want to use Swedish numbers.
But I don't! The file is from US so it contains decimal point instead of
decimal comma.

So I do this
double x = Convert.ToDouble(sX,
System.Globalization.NumberFormatInfo.InvariantInfo);

Which works, but ...

Isn't there someway of telling .NET that I always want to use this
NumberFormatInfo when dealing with numbers?

// Anders
 
Reply With Quote
 
 
 
 
kndg
Guest
Posts: n/a
 
      25th Jan 2011
On 1/25/2011 4:55 PM, Anders Eriksson wrote:
> Hello,
>
> I'm reading a text file that has some numbers in it (both integer and
> double). I then want to convert the string number into an integer or
> double.
>
> Since I'm in Sweden, .NET assumes that I want to use Swedish numbers.
> But I don't! The file is from US so it contains decimal point instead of
> decimal comma.
>
> So I do this
> double x = Convert.ToDouble(sX,
> System.Globalization.NumberFormatInfo.InvariantInfo);
>
> Which works, but ...
>
> Isn't there someway of telling .NET that I always want to use this
> NumberFormatInfo when dealing with numbers?
>
> // Anders


System.Threading.Thread.CurrentThread.CurrentCulture = new
System.Globalization.CultureInfo("en-US");
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      26th Jan 2011
On 25-01-2011 04:16, kndg wrote:
> On 1/25/2011 4:55 PM, Anders Eriksson wrote:
>> I'm reading a text file that has some numbers in it (both integer and
>> double). I then want to convert the string number into an integer or
>> double.
>>
>> Since I'm in Sweden, .NET assumes that I want to use Swedish numbers.
>> But I don't! The file is from US so it contains decimal point instead of
>> decimal comma.
>>
>> So I do this
>> double x = Convert.ToDouble(sX,
>> System.Globalization.NumberFormatInfo.InvariantInfo);
>>
>> Which works, but ...
>>
>> Isn't there someway of telling .NET that I always want to use this
>> NumberFormatInfo when dealing with numbers?

>
> System.Threading.Thread.CurrentThread.CurrentCulture = new
> System.Globalization.CultureInfo("en-US");


CultureInfo.InvariantCulture may match original
code better in intent. There should not be any
practical difference for this code.

Arne
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      26th Jan 2011
On 25-01-2011 03:55, Anders Eriksson wrote:
> I'm reading a text file that has some numbers in it (both integer and
> double). I then want to convert the string number into an integer or
> double.
>
> Since I'm in Sweden, .NET assumes that I want to use Swedish numbers.
> But I don't! The file is from US so it contains decimal point instead of
> decimal comma.


Even files from Sweden may use the IT style = English style.

> So I do this
> double x = Convert.ToDouble(sX,
> System.Globalization.NumberFormatInfo.InvariantInfo);
>
> Which works, but ...
>
> Isn't there someway of telling .NET that I always want to use this
> NumberFormatInfo when dealing with numbers?


kndg has told you about Thread.CurrentThread.CurrentCulture,
which answers your question.

I will just like to point out two unrelated points:

* if you use double.Parse instead of Convert.ToDouble, then
you ensure that type changes to sX will be noted by the
compiler - that may be a good thing

* depending on the data then you may prefer to use decimal
instead of double

Arne
 
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
Re: Converting a string of numbers to decimal Ken Microsoft Excel Programming 0 17th Jan 2007 08:47 PM
Re: Converting a string of numbers to decimal Don Guillett Microsoft Excel Programming 0 17th Jan 2007 08:43 PM
Converting String numbers into real numbers Werner Microsoft Excel Programming 1 27th Jun 2005 09:41 PM
Converting String numbers into real numbers Jean-Jérôme Doucet via OfficeKB.com Microsoft Excel Programming 2 27th Jun 2005 03:30 PM
Q: Converting a string with letters / numbers to hex Jon Gunnar Rue Microsoft VB .NET 1 25th Sep 2003 06:41 PM


Features
 

Advertising
 

Newsgroups
 


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