double toString

J

Jeff.Boeker

Hello,

I need to convert a double to a string such that there are a maximum of
two numbers after the decimal point with no trailing 0's or decimal
point (e.g. 3.456 = "3.46", 3.20 = "3.2", -123.001 = "-123").

Also I have to do a large number of these double-to-string conversions,
any speed optimization tips?

Thanks,

Jeff
 
J

Jon Shemitz

I need to convert a double to a string such that there are a maximum of
two numbers after the decimal point with no trailing 0's or decimal
point (e.g. 3.456 = "3.46", 3.20 = "3.2", -123.001 = "-123").

static string Fmt(double X)
{
return X.ToString(".##");
}

// Calling ToString directly is slightly faster than

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

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

Top