[Mono-list] Converting float to string for a MySQL db and internationalisation

Jaroslaw Kowalski jaak@zd.com.pl
Sat, 27 Dec 2003 18:18:30 +0100


There's also "CultureInfo.InvariantCulture" that you may use. It is
guaranteed to have dot as a decimal separator.

BTW. Setting thread culture in this case is not a good idea. You shoule
either use either of:

1. String.Format(CultureInfo.InvariantCulture, "INSERT INTO indexed
VALUES({0:#.##});", ...)
2. Convert.ToString(CultureInfo.InvariantCulture, ...).
3. Parametrized queries (this is actually the best idea because it solves
many problems you might not be even aware of + you can get some speed
improvements depending on your data provider).

Jarek

----- Original Message ----- 
From: <yoros@wanadoo.es>
To: "Sergio Blanco Cuaresma" <sergio-blanco@iespana.es>
Cc: "mono-list mailing list" <mono-list@lists.ximian.com>
Sent: Saturday, December 27, 2003 5:46 PM
Subject: Re: [Mono-list] Converting float to string for a MySQL db and
internationalisation


> On Sat, Dec 27, 2003 at 04:29:15PM +0100, Sergio Blanco Cuaresma wrote:
> > El sáb, 27-12-2003 a las 08:59, yoros@wanadoo.es escribió:
> > > On Fri, Dec 26, 2003 at 08:48:48PM +0100, Sergio Blanco Cuaresma
wrote:
> > > >
> > > > I have a little problem and I don't know how to solve it in an
elegant
> > > > way, due to internationalisation (my locales are "es_ES@euro") when
I
> > > > convert from a float type to string the decimal separator is ",",
thats
> > > > fantastic but I'm trying to insert that value in a MySQL database,
and
> > > > it seems to need "." as decimal separator:
> > > >
> > > > string sql = "INSERT INTO indexed VALUES ('" +
> > > > (fileInfo.Length).ToString("#.##") + "');";
> > > >
> > > > How would you solve this little problem? Is there any more elegant
way
> > > > of inserting data in a MySQL database? I'm using ByteFX.Data.
> > > >
> > >
> > > You can change DecimalSeparator to ".":
> > >
> > > using System.Globalization;
> > > ...
> > > NumberFormatInfo.CurrentInfo.NumberDecimalSeparator = ".";
> >
> > It seems to be read only:
> >
> > Excepcion: The current instance is read-only and a set operation was
> > attempted
> >
>
> Sorry, I didn't tested it. I have a code that does something like that I
> said...
>
>     using System.Threading;
> using System.Globalization;
> ...
>     CultureInfo ci = (CultureInfo) CultureInfo.CurrentCulture.Clone();
>     ci.NumberFormat = new NumberFormatInfo();
>     ci.NumberFormat.NumberDecimalSeparator = ",";
>     Thread.CurrentThread.CurrentCulture = ci;
>
> This works for me.
>
> Regards,
>
>     Pedro
>
> -- 
> Pedro Martínez Juliá
> \  yoros@terra.es
> )|    yoros@wanadoo.es
> /        http://yoros.dyndns.org
> Socio HispaLinux #311
> Usuario Linux #275438 - http://counter.li.org
> GnuPG public information:  pub  1024D/74F1D3AC
> Key fingerprint = 8431 7B47 D2B4 5A46 5F8E  534F 588B E285 74F1 D3AC
> _______________________________________________
> Mono-list maillist  -  Mono-list@lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
>