Search code examples
c#fixed-widthcurrency-formatting

C# Convert currency to string, remove decimal point but keep decimals, prepend 0s to have fixed width


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.


Solution

  • 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