1
votes

How do I convert a decimal value to a string of decimal digits (0-9), prefixed by a minus sign?

Decimal paymentAmt = -871.00M;

I need it formatted to: -000000871.00

The format parameter can be any valid standard numeric format specifier except for D, R, and X, as well as any combination of custom numeric format specifiers.

1

1 Answers

0
votes

You can use ToString to format it, check out MSDN for Custom Numeric Format Strings.

var paymentAmtFormatted = paymentAmt.ToString("000000000.00");
Console.WriteLine(paymentAmtFormatted);

The above prints the following to the Console:

-000000871,00

I am using the Zero Placeholder:

Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string.