473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Double.Parse - internalization problem

Hello

I need to convert some strings int doubles.

Unfortunetly strings do not follow my country culture standarts (we use
, to separate decimal from fraction, but strings use . there etc.).

Also, I have to convert those doubles back to strings, and I need to use
other standars then my culture (ie. use . instead of ,).

The first case (when using standard functions) causes exceptions. The
latter one proble is that ToString() is not good as it follows culture
standards.

How can I convert doubles to strings back and forth using custom culture
standard?

--
Semper Fidelis

Adam Klobukowski
at***@gabo.pl
Nov 16 '05 #1
5 12613
Adam Klobukowski <at***@gabo.pl> wrote:
I need to convert some strings int doubles.

Unfortunetly strings do not follow my country culture standarts (we use
, to separate decimal from fraction, but strings use . there etc.).

Also, I have to convert those doubles back to strings, and I need to use
other standars then my culture (ie. use . instead of ,).

The first case (when using standard functions) causes exceptions. The
latter one proble is that ToString() is not good as it follows culture
standards.

How can I convert doubles to strings back and forth using custom culture
standard?


Use the version of Double.Parse that takes an IFormatProvider, and pass
in the the appropriate CultureInfo. Similarly give the appropriate
CultureInfo to Double.ToString.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
You want to use System.Globalization.NumberFormatInfo.
Example:
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberGroupSeparator = ",";
string s = "3.1415";
double d = double.Parse(s, nfi);
Console.WriteLine(d);

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Adam Klobukowski" <at***@gabo.pl> wrote in message
news:cn**********@atlantis.news.tpi.pl...
Hello

I need to convert some strings int doubles.

Unfortunetly strings do not follow my country culture standarts (we use ,
to separate decimal from fraction, but strings use . there etc.).

Also, I have to convert those doubles back to strings, and I need to use
other standars then my culture (ie. use . instead of ,).

The first case (when using standard functions) causes exceptions. The
latter one proble is that ToString() is not good as it follows culture
standards.

How can I convert doubles to strings back and forth using custom culture
standard?

--
Semper Fidelis

Adam Klobukowski
at***@gabo.pl

Nov 16 '05 #3
You want the overload of Double.Parse that accepts both a string and an
IFormatProvider.

You can pass in an instance of CultureInfo as the IFormatProvider
implementation. Consider this example:

CultureInfo uk = new CultureInfo("en-GB");
CultureInfo france = new CultureInfo("fr-FR");

string strVal = "123,456";
Console.WriteLine(Double.Parse(strVal));
Console.WriteLine(Double.Parse(strVal, uk));
Console.WriteLine(Double.Parse(strVal, france));
On my machine (which is running in a UK locale), I get:

123456
123456
123.456

as output. So you can see that the one-param version of Double.Parse has
used the local convention for interpretting "," which is that it's a digit
grouping indicator, rather than a decimal point. Passing in an explicit UK
locale has had the same result.

But passing in a French CultureInfo has caused it to treat the "," as a
decimal point.

You could even write your own IFormatProvider implementation, but I would
only do that if there is no culture that meets your requirements.
--
Ian Griffiths - http://www.interact-sw.co.uk/iangblog/
DevelopMentor - http://www.develop.com/

"Adam Klobukowski" <at***@gabo.pl> wrote:

I need to convert some strings int doubles.

Unfortunetly strings do not follow my country culture standarts (we use ,
to separate decimal from fraction, but strings use . there etc.).

Also, I have to convert those doubles back to strings, and I need to use
other standars then my culture (ie. use . instead of ,).

The first case (when using standard functions) causes exceptions. The
latter one proble is that ToString() is not good as it follows culture
standards.

How can I convert doubles to strings back and forth using custom culture
standard?

Nov 16 '05 #4
Dennis Myrén napisa³(a):
You want to use System.Globalization.NumberFormatInfo.
Example:
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberGroupSeparator = ",";
string s = "3.1415";
double d = double.Parse(s, nfi);
Console.WriteLine(d);


Thanks. That was exactly what I needed.

--
Semper Fidelis

Adam Klobukowski
at***@gabo.pl
Nov 16 '05 #5
By the way, double to string would be:
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberGroupSeparator = ".";

double d = 3.1415;
Console.WriteLine(d.ToString(nfi));

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Adam Klobukowski" <at***@gabo.pl> wrote in message
news:cn**********@atlantis.news.tpi.pl...
Dennis Myrén napisa³(a):
You want to use System.Globalization.NumberFormatInfo.
Example:
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberGroupSeparator = ",";
string s = "3.1415";
double d = double.Parse(s, nfi);
Console.WriteLine(d);


Thanks. That was exactly what I needed.

--
Semper Fidelis

Adam Klobukowski
at***@gabo.pl

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jesper Denmark | last post by:
Hi, I use the double.Parse functionality to convert a number in a text file into a double. However, while this works fine on one computer it doesn't on another. I've found out that it is...
1
by: Kevin | last post by:
Why do I get different values for the same hex string when parsing to an integer versus a double? See the follwoing code snippet: using System; using System.Collections; using...
3
by: slylos | last post by:
I've got a section of code in my app that keeps track of how much time an employee has accrued, which equals out to 1.33 days of PTO per month. I'm trying to store the time accrued so far in an...
5
by: Markus Kling | last post by:
"double.Parse(double.MaxValue.ToString())" yields the following Exception: Value was either too large or too small for a Double. at System.Number.ParseDouble(String value, NumberStyles options,...
4
by: Daniel Walzenbach | last post by:
Hi, I wonder if somebody could explain me the difference between Double.Parse and Convert.ToDouble. If I'm not mistaken they are implemented differently (I though for a moment they might be the same...
2
by: Samuel R. Neff | last post by:
I'm using a quasi open-source project and am running into an exception in double.Parse which is effectively this: double.Parse(double.MinValue.ToString()) System.OverflowException: Value was...
1
by: schaf | last post by:
Hi NG! I have a little question about the localization of a double. If I use the CultureInfo "en-US" I have the following behavior: 1.) If I enter the value 2.5 into a textbox the...
5
by: schlied | last post by:
Hello, i have the following problem in our project: If I try to parse a string value with the following line double x = double.Parse("39.95238", CultureInfo.InvariantCulture); x contains...
2
by: Mika M | last post by:
Hi, Just for fun I'm trying to parse my GPS position string using C# 2005. When my code is trying to parse latitude string to double value like... double.Parse(items); // items = "6215.1058"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.