-8

How do I convert money amount like this

38.50

, to fixed width like this

000003850

I dont want to add commas and decimal places as some suggested answers here are explaining how to do. Neither do I want to remove decimals, I only want to remove decimal point.

cd491415
  • 823
  • 2
  • 14
  • 33

2 Answers2

2

You can use string.Format() with custom format specifiers. See details here.

double dollars = 38.50;   // your value
int temp = (int)(dollars * 100);   // multiplication to get an integer
string result = string.Format("{0:000000000}", temp);

// Output: 000003850
manaszon
  • 308
  • 2
  • 8
0

Figured it out thanks to @ReedCopsey at How to remove decimal point from a decimal number in c#?.

decimal amount = 38.50m;
int fixed_amount = (int)Math.Truncate(amount * 100);
Console.WriteLine(fixed_amount.ToString("D9"));

OUTPUT:

000003850
E_net4
  • 27,810
  • 13
  • 101
  • 139
cd491415
  • 823
  • 2
  • 14
  • 33