Double format without decimal point

I need to convert double to 10 digits + 4 decimal numbers.

so say: I have a double 999.56. I should get 00000009995600 -> without a comma!

I have:

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

so now I can find the comma and delete it, but I would like to know if there is another “clean” way of formatting in this particular format?

Thank!

+3
source share
5 answers

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 there is really nothing wrong with that:

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"
+4
source
string.Format("{0:00000000000000}", value * 10000)
+15
source

, Replace(), , "" :

, :

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

!

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

If you don't want (or can't) breed, this might work:

string newValue = value.Replace(",", "");
-2
source

Source: https://habr.com/ru/post/1725336/


All Articles