Reputation: 37
is there a way I can format a numeric decimal number to a 12 digit ?
I have to integrate the project with a payment gateway and the requirement says that
The amount will have to be padded with "0" from the left and include no decimal point. Ex : 1 = 000000000100 or 1.5 = 000000000150
So far, I have Convert.ToDecimal(amount).ToString().PadLeft(12, '0');
but this gives me 000000000015. I tried to .PadRight also but that doesn't give out the result.
Thank you
Upvotes: 4
Views: 78
Reputation: 5068
and the
- Decimal("D") Format Specifier (pads zeroes);
- same page, code sample.
int value;
value = 12345;
Console.WriteLine(value.ToString("D"));
// Displays 12345
Console.WriteLine(value.ToString("D8"));
// Displays 00012345
Upvotes: 1
Reputation: 116
You can also give this a try.
Convert.ToDecimal(amount).ToString("0000000000.00").Replace(".", "");
Upvotes: 1
Reputation: 19149
Just multiply your number by 100.
Convert.ToDecimal(amount*100).ToString().PadLeft(12, '0')
Upvotes: 3