Alvin Alvin - 3 months ago 21
C# Question

How to format string to money

I have a string like

000000000100
, which I would like to convert to 1.00 and vice versa.

Leading zero will be remove, last two digit is the decimal.

I give more example :

000000001000 <=> 10.00
000000001005 <=> 10.05
000000331150 <=> 3311.50


Below is the code I am trying, it is giving me result without decimal :

amtf = string.Format("{0:0.00}", amt.TrimStart(new char[] {'0'}));

Answer

Convert the string to a decimal then divide it by 100 and apply the currency format string:

string.Format("{0:#.00}", Convert.ToDecimal(myMoneyString) / 100);

Edited to remove currency symbol as requested and convert to decimal instead.

Comments