views:

343

answers:

4

Hi,

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 if there is another "clean" way of formatting in this specific format?

Thanks!

+12  A: 
string.Format("{0:00000000000000}", value * 10000)
Stu
Well, that was clear for me too, but the problem is that I've no idea about the value that I revive! It can also be only 1.898 or xx.x or xxxxx.xxx or whatever!
Gohlool
So? Have you tried it?
Stu
A: 

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

string newValue = value.Replace(",", "");
+1  A: 

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"
Jason
Thanks! I did the same thing for some minutes ago since I didn't find anything else! I also used the CulterInfo, but didn't call GetCulterInfo(). Here is what I did:string sep = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;Console.WriteLine(String.Format(fromt, testvalue).Replace(sep, ""));Thanks!
Gohlool
A: 

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!

Gohlool