How to format string to money

C#

C# Problem Overview


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'}));

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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

EX:

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

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

Solution 3 - C#

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:

Solution 4 - C#

It works!

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

Output

Order Total: $1,921.39

Solution 5 - C#

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

Solution 6 - C#

Try simple like this

 var amtf = $"{Convert.ToDecimal(amt):#0.00}";

Solution 7 - C#

    string s ="000000000100";
    decimal iv = 0;
    decimal.TryParse(s, out iv);
    Console.WriteLine((iv / 100).ToString("0.00"));

Solution 8 - C#

//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");

Solution 9 - C#

you can also do :

string.Format("{0:C}", amt)

Solution 10 - C#

Try something like this:

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

Solution 11 - C#

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));
}

Solution 12 - C#

Parse to your string to a decimal first.

Solution 13 - C#

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"

Solution 14 - C#

try

amtf =  amtf.Insert(amtf.Length - 2, ".");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlvinView Question on Stackoverflow
Solution 1 - C#Lloyd PowellView Answer on Stackoverflow
Solution 2 - C#Mohammed SwillamView Answer on Stackoverflow
Solution 3 - C#Bhanu PratapView Answer on Stackoverflow
Solution 4 - C#Jorge Luis Peña LopezView Answer on Stackoverflow
Solution 5 - C#Aaron ShermanView Answer on Stackoverflow
Solution 6 - C#SkyKingView Answer on Stackoverflow
Solution 7 - C#HabibView Answer on Stackoverflow
Solution 8 - C#Piotr JustynaView Answer on Stackoverflow
Solution 9 - C#JackalView Answer on Stackoverflow
Solution 10 - C#thejartenderView Answer on Stackoverflow
Solution 11 - C#BloggrammerView Answer on Stackoverflow
Solution 12 - C#greijnerView Answer on Stackoverflow
Solution 13 - C#Antony ScottView Answer on Stackoverflow
Solution 14 - C#PeraView Answer on Stackoverflow