PC Review


Reply
Thread Tools Rate Thread

Decimal seperator

 
 
Peter Larsen []
Guest
Posts: n/a
 
      7th May 2007
Hi,

How do i convert a string to float/double if the decimal-seperator doesn't
follow the local language settings ??

Eg.
string s = load a value from a file - "1234,12";
float f = float.Parse(s);

This will fail because my PC expect a dot - not a comma.

/Peter


 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      7th May 2007
Peter,

You can create a NumberFormatInfo instance and set the
NumberGroupSeparator to the comma character, and then pass it to the Parse
method, and it should parse it correctly.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"Peter Larsen []" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> How do i convert a string to float/double if the decimal-seperator doesn't
> follow the local language settings ??
>
> Eg.
> string s = load a value from a file - "1234,12";
> float f = float.Parse(s);
>
> This will fail because my PC expect a dot - not a comma.
>
> /Peter
>



 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      8th May 2007
Peter Larsen [] wrote:
> How do i convert a string to float/double if the decimal-seperator doesn't
> follow the local language settings ??
>
> Eg.
> string s = load a value from a file - "1234,12";
> float f = float.Parse(s);
>
> This will fail because my PC expect a dot - not a comma.


You can specify local explicit:

float f = float.Parse(s, new CultureInfo("da-DK", false));

Arne
 
Reply With Quote
 
Linda Liu [MSFT]
Guest
Posts: n/a
 
      8th May 2007
Hi Peter,

Nicholas has pointed out the right direction.

We could use NumberFormatInfo to solve this problem, i.e. create a
NumberFormatInfo instance and set its NumberDecimalSeparator property to
","(perhaps Nicholas miswrote the NumberDecimalSeparator to
NumberGroupSeparator).

The following is a sample.

NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberDecimalSeparator = ",";
float f = float.Parse("1234,12", nfi);

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

 
Reply With Quote
 
Peter Larsen []
Guest
Posts: n/a
 
      8th May 2007
Thank you all for your comments.

I will try the sample from Linda:

NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberDecimalSeparator = ",";
float f = float.Parse("1234,12", nfi);


/Peter


 
Reply With Quote
 
Linda Liu [MSFT]
Guest
Posts: n/a
 
      10th May 2007
Hi Peter,

How about the problem now?

If you have any question, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support

 
Reply With Quote
 
Peter Larsen []
Guest
Posts: n/a
 
      11th May 2007
Hi Linda,
Thank you for the answer.

BR
Peter


 
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
TextBox only numbers and decimal seperator Joza Microsoft C# .NET 1 24th Feb 2011 11:50 AM
Change decimal seperator in VBA to dot Helge Microsoft Access 1 11th May 2009 02:47 PM
Thousand/Decimal seperator Gertjan Huiskes Microsoft Excel Programming 3 16th Oct 2008 03:55 AM
Thousand/Decimal seperator Gertjan Huiskes Microsoft Excel Worksheet Functions 3 16th Oct 2008 03:55 AM
Decimal seperator and Doubles in an Update Query Ward Visser Microsoft Access Queries 4 6th Nov 2003 09:06 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:31 PM.