.NET Framework - Double to String. Force the use of Dot
Asked By shapper
10-Feb-10 05:03 PM
Hello,
I am converting a double (4.2) to string and I get "4,0".
I would like to get always "4.0". With a dot instead of with a comma.
How can I do this? I am using ToString() ...
I can always use String.Replace but probably there might be a better
way or not?
Thanks,
Miguel
CultureInfo.InvariantCulture.NumberFormat
(1)
UTF8Encoding
(1)
CultureInfo.InvariantCulture
(1)
IFormatProvider
(1)
ConformanceLevel.Document
(1)
Environment.NewLine
(1)
WriteStartElement
(1)
XmlWriter.Create
(1)
Stefan Hoffmann replied to shapper
hi,
Use a format provider:
http://msdn.microsoft.com/en-us/library/d8ztz0sa.aspx
mfG
--> stefan <--
Peter Duniho replied to Stefan Hoffmann
In particular, if you always want US-style strings even on
non-US-configured computers, passing CultureInfo.InvariantCulture as the
IFormatProvider is usually what you want to do. For example:
double d = 4.2;
string str = d.ToString(CultureInfo.InvariantCulture);
Pete
Harlan Messinger replied to shapper
double x = 4.2;
x.ToString(CultureInfo.InvariantCulture.NumberFormat);
Peter Duniho replied to Harlan Messinger
CultureInfo implements IFormatProvider. You do not need the
shapper replied to Peter Duniho

I did try it by in the XML File I am creating I always get the comma
and not the not.
Here is my entire code:
MemoryStream stream =3D new MemoryStream();
// Define settings
var settings =3D new XmlWriterSettings {
Encoding =3D new UTF8Encoding(false),
ConformanceLevel =3D ConformanceLevel.Document,
Indent =3D true
};
// Open writer
XmlWriter writer =3D XmlWriter.Create(stream, settings);
writer.WriteStartElement("urlset", "http://www.sitemaps.org/
schemas/sitemap/0.9");
writer.WriteWhitespace(Environment.NewLine);
// Fill write
foreach (SitemapUrl u in Urls) {
writer.WriteStartElement("url");
writer.WriteElementString("loc", u.Location);
writer.WriteElementString("lastmod", u.Updated.ToString("yyyy-
MM-dd"));
writer.WriteElementString("changefreq",
u.ChangeFrequency.ToString().ToLower());
writer.WriteElementString("priority", String.Format("{0:0.0}",
(Double)u.Priority, CultureInfo.InvariantCulture));
writer.WriteEndElement();
writer.WriteWhitespace(Environment.NewLine);
}
// Close writer
writer.WriteWhitespace(Environment.NewLine);
writer.WriteEndElement();
writer.Flush();
// Define content
StringBuilder content =3D new StringBuilder();
content.Append(Encoding.UTF8.GetString(stream.ToArray()));
return content.ToString();
Check the priority ... I am trying to create string with only one
decimal from the double.
I get always the comma on the XML file.
If i just use a string "0.4" then it works fine ...
Any idea?
Thanks,
Miguel
Peter Duniho replied to shapper
Sure. You are not calling the method that all three of us have
suggested. Instead, you are passing two objects to the String.Format()
method, only the first of which is ever even used (String.Format() has
no way to know you wanted to use the culture object passed in as a
format provider?as far as it knows, it is just another object to include
in the format, except your format string does not refer it).
Change this:
writer.WriteElementString("priority", String.Format("{0:0.0}",
(Double)u.Priority, CultureInfo.InvariantCulture));
To this:
writer.WriteElementString("priority",
((Double)u.Priority).ToString("0.0", CultureInfo.InvariantCulture));
Hope that helps.
Pete
shapper replied to Peter Duniho
Thank You Pete! Once Again!
Cheers,
Miguel
Arne_Vajhøj replied to Peter Duniho
I would find it tempting to use the flavor:
writer.WriteElementString("priority",
String.Format(CultureInfo.InvariantCulture, "{0:0.0}", u.Priority));
Arne
Peter Duniho replied to Arne_Vajhøj
To each his own. I see no real practical difference in this particular
case, and in other situations the call to String.Format() might require
boxing of a value type that otherwise would not have to happen if one
called ToString() directly.
But the code works fine either way. :)

I for example replace this statement result = firstOperand + secondOperand; with this SetTextValue((firstOperand + secondOperand).ToString(CultureInfo.InvariantCulture)); it doesn't work because ToString doesn't appear in intellisense when I enter the secondOperand; break; case '*': result = firstOperand * secondOperand; break; case ' / ': result = firstOperand / secondOperand; break; } SetTextValue(result.ToString(CultureInfo.InvariantCulture)); selectedOperator = ' \ 0'; } private void SetTextValue(string textValue) { txtDisplay.Text = textValue; } / / Tony C# Discussions Visual Studio (1) CultureInfo.InvariantCulture (1) System.Collections.Generic (1) SetTextValue (1) Main (1) ConsoleApplication1 (1) If when you type on the result. -Scott Hello! I mean that in this expression SetTextValue((firstOperand + secondOperand).ToString(CultureInfo.InvariantCulture)); should firstOperand be added to secondOperand which firstOperand + secondOperand is saying then the decimal value
simple console application with about 10 rows .NET Framework Hi! I wonder when I pass CultureInfo.InvariantCulture as an argument to the c-tor of new CaseInsensitiveComparer below what effect does it mean the same thing public static void Main() { 1 ListDictionary myList = new ListDictionary(new CaseInsensitiveComparer(CultureInfo.InvariantCulture)); 2 ListDictionary myList = new ListDictionary(new CaseInsensitiveComparer()); myList["Estados Unidos"] = "United states"; myList["Canada"] = "Canada keywords: simple, console, application, with, about, 10, rows description: Hi! I wonder when I pass CultureInfo.InvariantCulture as an argument to the c-tor of new CaseInsensitiveComparer below what effect does it
an overload for ToString which takes a CultureInfo as a parameter, so I can specify CultureInfo.InvariantCulture. The Object type itself though does not have this overload so I can't just do this: MyString = MyObj.ToString(CultureInfo.InvariantCulture) because it won't compile. I have managed to work around this problem like this MyString = CallByName(MyObj, "ToString", CallType.Method, CultureInfo.InvariantCulture) but I feel there ought to be a 'better' way to do this. Any suggestions appreciated. TIA Phil. VB.NET Discussions CultureInfo.InvariantCulture (1) IFormattable.Armin (1) CallType.Method (1) CallByName (1) CallType (1) IFormattable (1) IConvertible (1