Double to String. Force the use of Dot

S

shapper

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
 
H

Harlan Messinger

shapper said:
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

double x = 4.2;
x.ToString(CultureInfo.InvariantCulture.NumberFormat);
 
S

shapper

CultureInfo implements IFormatProvider.  You don't need the
".NumberFormat".  :)

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 = new MemoryStream();

// Define settings
var settings = new XmlWriterSettings {
Encoding = new UTF8Encoding(false),
ConformanceLevel = ConformanceLevel.Document,
Indent = true
};

// Open writer
XmlWriter writer = 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 = 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
 
P

Peter Duniho

shapper said:
[...]
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.

Sure. You aren't calling the method that all three of us have
suggested. Instead, you're 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's just another object to include
in the format, except your format string doesn't 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
 
S

shapper

shapper said:
[...]
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.

Sure.  You aren't calling the method that all three of us have
suggested.  Instead, you're 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's just another object to include
in the format, except your format string doesn't 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

Thank You Pete! Once Again!

Cheers,
Miguel
 
A

Arne Vajhøj

shapper said:
[...]
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.

Sure. You aren't calling the method that all three of us have suggested.
Instead, you're 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's just another object to include in the format,
except your format string doesn't 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));

I would find it tempting to use the flavor:

writer.WriteElementString("priority",
String.Format(CultureInfo.InvariantCulture, "{0:0.0}", u.Priority));

Arne
 
P

Peter Duniho

Arne said:
[...]
writer.WriteElementString("priority",
((Double)u.Priority).ToString("0.0", CultureInfo.InvariantCulture));

I would find it tempting to use the flavor:

writer.WriteElementString("priority",
String.Format(CultureInfo.InvariantCulture, "{0:0.0}", u.Priority));

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 wouldn't have to happen if one
called ToString() directly.

But the code works fine either way. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Remove characters from string 2
String. Get words ... 4
Converting string numbers to numbers 3
Regular expression 4
Double NAN to 0 1
String Array to List 4
String to DateTime 1
Linq. String 5

Top