103

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'}));
Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
Alvin
  • 8,219
  • 25
  • 96
  • 177

15 Answers15

88

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.

Lloyd Powell
  • 18,270
  • 17
  • 87
  • 123
74

you will need to convert it to decimal first, then format it in money format.

EX:

decimal decimalMoneyValue = 1921.39m;
string formattedMoneyValue = String.Format("{0:C}", decimalMoneyValue);

a working example: https://dotnetfiddle.net/soxxuW

Mohammed Swillam
  • 9,119
  • 4
  • 36
  • 47
55
decimal value = 0.00M;
value = Convert.ToDecimal(12345.12345);
Console.WriteLine(value.ToString("C"));
//OutPut : $12345.12
Console.WriteLine(value.ToString("C1"));
//OutPut : $12345.1
Console.WriteLine(value.ToString("C2"));
//OutPut : $12345.12
Console.WriteLine(value.ToString("C3"));
//OutPut : $12345.123
Console.WriteLine(value.ToString("C4"));
//OutPut : $12345.1234
Console.WriteLine(value.ToString("C5"));
//OutPut : $12345.12345
Console.WriteLine(value.ToString("C6"));
//OutPut : $12345.123450

Console output:

Pang
  • 9,564
  • 146
  • 81
  • 122
Bhanu Pratap
  • 1,635
  • 17
  • 17
  • 1
    This one will properly respect culture variance. Cultural differences can not only change the symbol, but the digit separators as well (and theoretically the position of the currency symbol). https://msdn.microsoft.com/en-us/library/syy068tk(v=vs.90).aspx – Robear Aug 22 '17 at 17:48
23

It works!

decimal moneyvalue = 1921.39m; 
string html = String.Format("Order Total: {0:C}", moneyvalue); 
Console.WriteLine(html);

Output

Order Total: $1,921.39
Liam
  • 27,717
  • 28
  • 128
  • 190
17

Once you have your string in a double/decimal to get it into the correct formatting for a specific locale use

double amount = 1234.95;

amount.ToString("C") // whatever the executing computer thinks is the right fomat

amount.ToString("C", System.Globalization.CultureInfo.GetCultureInfo("en-ie"))    //  €1,234.95
amount.ToString("C", System.Globalization.CultureInfo.GetCultureInfo("es-es"))    //  1.234,95 € 
amount.ToString("C", System.Globalization.CultureInfo.GetCultureInfo("en-GB"))    //  £1,234.95 

amount.ToString("C", System.Globalization.CultureInfo.GetCultureInfo("en-au"))    //  $1,234.95
amount.ToString("C", System.Globalization.CultureInfo.GetCultureInfo("en-us"))    //  $1,234.95
amount.ToString("C", System.Globalization.CultureInfo.GetCultureInfo("en-ca"))    //  $1,234.95
Aaron Sherman
  • 3,789
  • 1
  • 30
  • 33
5

Try simple like this

 var amtf = $"{Convert.ToDecimal(amt):#0.00}";
SkyKing
  • 101
  • 1
  • 6
4
//Extra currency symbol and currency formatting: "€3,311.50":
String result = (Decimal.Parse("000000331150") / 100).ToString("C");

//No currency symbol and no currency formatting: "3311.50"
String result = (Decimal.Parse("000000331150") / 100).ToString("f2");
Piotr Justyna
  • 4,888
  • 3
  • 25
  • 40
4
    string s ="000000000100";
    decimal iv = 0;
    decimal.TryParse(s, out iv);
    Console.WriteLine((iv / 100).ToString("0.00"));
Habib
  • 219,104
  • 29
  • 407
  • 436
2

you can also do :

string.Format("{0:C}", amt)
Ethaan
  • 11,291
  • 5
  • 35
  • 45
Jackal
  • 2,591
  • 3
  • 25
  • 29
1

Try something like this:

decimal moneyvalue = 1921.39m;
string html = String.Format("Order Total: {0:C}", moneyvalue);
Console.WriteLine(html);
thejartender
  • 9,339
  • 6
  • 34
  • 51
1

In my case, I used this string format to display currency from decimal values without the symbol.

String format:

string.Format("{0:#,##0.00}", decimalValue)

Example:

var decimalNumbers = new decimal[] { 1M, 10M, 100M, 1000M,10000M,100000M,1000000M,1000000000M };          

foreach (var decimalNumber in decimalNumbers)
{
   Console.WriteLine(string.Format("{0:#,##0.00}", decimalNumber));
}
Bloggrammer
  • 921
  • 8
  • 21
0

Parse to your string to a decimal first.

greijner
  • 299
  • 1
  • 3
  • 10
0
var tests = new[] {"000000001000", "000000001005", "000000331150"};
foreach (var test in tests)
{
    Console.WriteLine("{0} <=> {1:f2}", test, Convert.ToDecimal(test) / 100);
}

Since you didn't ask for the currency symbol, I've used "f2" instead of "C"

Antony Scott
  • 21,690
  • 12
  • 62
  • 94
0

try

amtf =  amtf.Insert(amtf.Length - 2, ".");
Pera
  • 1
  • that doesn't remove the leading zeros. If you're going down the simple string manipulation route you would need to add .Trim("0".ToCharArray()) to the end of your code – Antony Scott May 16 '12 at 09:26
0
 private string cxp(string txt) {
        try
        {

            decimal n;
            n = Math.Round( Convert.ToDecimal( txt),2);

            string newTxt;
            newTxt = Convert.ToString(n);
            //txt = txt.Replace(",", ".");
            //string newtxt = string.Format("{0:#.00}", Convert.ToDecimal(txt) );

            return newTxt.Replace(",", ".");

        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message ,"Error al parsear número");
            //throw;
            return txt;
        }


    }
R.Alonso
  • 989
  • 1
  • 8
  • 9