开发者

Format double with no decimal point

I've to convert double to 10 digits + 4 decimal.

so let say: I have a double 999,56. I've to get 00000009995600 -> without comma!

What I have is:

string.format("{0:0000000000.0000}", value) but what I get is: 0000000999,5600

so what I can do now is to search for the comma and delete it, but I would like to know开发者_StackOverflow中文版 if there is another "clean" way of formatting in this specific format?

Thanks!


string.Format("{0:00000000000000}", value * 10000)


Since you said you want to avoid String.Replace:

// avoid whacked out cultures
double value = 999.56m;
CultureInfo info = CultureInfo.GetCultureInfo("en-US");
string s = (10000 * value).ToString("00000000000000", info));
Console.WriteLine(s); // displays "00000009995600"

But really, there's nothing wrong with it:

double value = 999.56m;
CultureInfo info = CultureInfo.GetCultureInfo("en-US");
string s = value.ToString("0000000000.0000", info).Replace(
    info.NumberFormat.NumberDecimalSeparator,
    String.Empty)
Console.WriteLine(s); // displays "00000009995600"


Well, I will use the Replace() method since I guess there is no other "simple" way:

Here is my solution which is almost the same as Jason's answer:

string sep = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
Console.WriteLine(String.Format(fromt, testvalue).Replace(sep, ""));

Thanks!


double val = 12345.6789;
return val.ToString().Split('.')[0];


If you don't want to (or can't) multiply then this may work:

string newValue = value.Replace(",", "");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜