[Mono-list] Numbers with ","

Jonathan Pryor jonpryor at vt.edu
Sat Feb 11 11:07:13 EST 2006


On Sat, 2006-02-11 at 00:56 +0000, Paulo Augusto wrote:
> In portuguese (in fact, every language i know except english), numbers
> are separated by a "," comma, instead of by a "." dot.
> 
> So, when my mono program needs a number from an Entry():
> Convert.ToSingle (string);
> and then i want to pass that number to MySql through an sql string:
> float.ToString();
> results in a string that has a "," which brakes the sql string.
> 
> Anyone has any idea on what's the best way to handle this localisation
> issue?

I quite literally just answered this question:

http://lists.ximian.com/pipermail/mono-list/2006-February/030647.html

Too bad the other half of my answer didn't make it to the list.  :-/

To go from a number within your current culture to the equivalent value
in a different culture requires two steps: (1) parse the string into a
number; and (2) format the number into an appropriate string:


        NumberFormatInfo source = 
	        new CultureInfo ("de-DE").NumberFormat;
	        // or use your appropriate culture instead of "de-DE",
                // or use CultureInfo.InstalledUICulture.NumberFormat
        NumberFormatInfo dest =
                CultureInfo.InvariantCulture.NumberFormat;
                // or use `new CultureInfo ("en-US").NumberFormat

	// (1) Parse the string into a double:
	double d = double.Parse ("12,34", source);

	// (2) Create a string for use in SQL insertion:
	string sql = d.ToString (dest);

That's all you need to do, and this will work properly for all languages
(assuming you use the correct source NumberFormatInfo), as opposed to
just replacing all instances of ',' with '.' (which will fail if you
ever get thousands-grouping symbols, e.g. "123,456,789.01", though you
probably don't want grouping symbols for SQL insertion anyway...).

 - Jon




More information about the Mono-list mailing list