Best way to achieve complex number formatting in C #

What is the best way to reproduce the behavior of something like this in C #?

// Converts decimal to a 0-padded string with minimum specified width and precision.
sprintf(out, "%0*.*lf", width, precision, decimal);

I have read the standard , custom and compound format strings, but I don't see anything good to achieve this.

The reason I ask is because I just stumbled upon this ugly piece of code in some code that I support where we need to format a bunch of decimal places according to the width and precision of the variables, as indicated by the external interface :

private static String ZEROS = "00000000000000000000000000";

public override string Format(decimal input, int width, int precision)
{
    String formatString = ZEROS.Substring(0, width - precision - 1) 
                          + "." + ZEROS.Substring(0, precision);
    return ToDecimal(input).ToString(formatString);
}

and I would like to replace it with something less terrible.

UPDATE

Since the final answer is buried in the comments, here it is:

public override string Format(decimal input, int width, int precision)
{
    string.Format("{0," + width + ":F" + precision + "}", input).Replace(" ","0");
}

In my case, the input is always positive, so this also works:

public override string Format(decimal input, int width, int precision)
{
    return input.ToString("F"+precision).PadLeft(width, 0);
}
+3
5

, , :

public static string Format(decimal input, int width, int precision)
{
    var format = string.Format("{{0,{0}:F{1}}}", width, precision);
    // generates (e.g. width=15, precision=5)  "{0,15:F5}"
    // then format using this and replace padding with zeroes
    return string.Format(format, input).Replace(" ", "0");
}

, , , :

string.Format("{0," + width + ":F" + precision + "}", input).Replace(" ","0")

, . . , :)

: , , precision = 0. , , .

: .

, , , :

public override string Format(decimal input, int width, int precision)
{
    var output = input.ToString("F" + precision);
    return input < 0
        ? "-" + output.Substring(1).PadLeft(width, '0')
        :       output             .PadLeft(width, '0');
}
+3

- :

return input.ToString(new string('0', width) + '.' + new string('0', precision));

?

: , :

return input.ToString(new string('0', width - 1).insert(width - precision, '.'));
+1

Well, one way to get rid of ZEROwould be

public override string Format(decimal input, int width, int precision)
{
    String formatString = new string('0', width - precision - 1) 
                      + "." + new string('0', precision);
    return ToDecimal(input).ToString(formatString);
}

Not sure you can do much more.

0
source

This is about as good as it gets:

public static string double2string( double value , int integerDigits , int fractionalDigits )
{
  string        decimalPoint = CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator ;
  StringBuilder sb           = new StringBuilder() ;

  sb.Append('0',integerDigits)
    .Append(decimalPoint)
    .Append('0',fractionalDigits)
    ;
  return value.ToString( sb.ToString() ) ;
}
0
source

I believe that you can just call Decimal.ToStringand pass it a Custom Numeric Format String that you can create.

-1
source

All Articles