Problem with double to string conversion c#

396 views
Skip to first unread message

Simon.u

unread,
Jun 2, 2009, 7:29:07 AM6/2/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi,
I tried to use the

String.Format("{0:0.####}", double_number);

method in my c# application but I have a problem.
I have an application developed with Visual Studio 2008, using c#. It
has to communicate with an activeX shockwave flash object, sending a
double variable. Since the only way to make c# and flash communicate
is the SetVariable method, which supports only the string type, I have
to pass my double variable from c# to flash, converting it to a string
e then reconverting it to the Number type in flash.
The double ToString() method doesn't work properly, i.e. it gives me a
string with the floating point
indicated with a ",", while flash requires a decimal number with the
dot. But the String.Format method gives me the same result, even if I
set the "." in the regular expression.

I try to explain it better with an example:

string s = String.Format("{0:0.####}", 30.54);
MessageBox.Show(s);

it shows me the string "30,54" in the message box and not the "30.54"
one. So when I pass it to flash, it doesn't recognize the number, and
cannot convert it properly.
I'm using the italian version of VS2008, so maybe that is due to an
internal italian representation of decimal numbers?
Can anyone give me any help?
Thank you in advance.
Greetings.
Simon.u

Cerebrus

unread,
Jun 2, 2009, 12:45:01 PM6/2/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
You are correct in your suspicion that the comma as a decimal
separator is due to the regional settings configured upon your version
of Windows. (Regional settings).

The String.Format method never ceases to amaze me with it's power, in
this case, by virtue of being culture sensitive. IOW, It formats
values according to your culture. In your case, the problem arises not
because .NET is creating the decimal separator as a comma, but because
your version of Flash is not complying with your system settings. (I
don't develop shockwave... I have no idea if it even has this feature)

However, since you wish to override this default behaviour of the
String.Format method, you can use the overload that specifies an
IFormatProvider:

---
string s = String.Format(NumberFormatInfo.InvariantInfo, "{0:0.####}",
30.54);
---

Setting it to the Invariant culture format ensures that values will be
formatted according to en-US (the programmers' universal
culture! ;-)).
Reply all
Reply to author
Forward
0 new messages