Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format string to money

Tags:

c#

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'})); 
like image 907
Alvin Avatar asked May 16 '12 09:05

Alvin


People also ask

What is the ToString format code for currency?

The "C" (or currency) format specifier is used to convert a number to a string representing a currency amount. Let us see an example. double value = 139.87; Now to display the above number until three decimal places, use (“C3”) currency format specifier.

How do you convert numbers to money format?

On the Home tab, click the Dialog Box Launcher next to Number. Tip: You can also press Ctrl+1 to open the Format Cells dialog box. In the Format Cells dialog box, in the Category list, click Currency or Accounting. In the Symbol box, click the currency symbol that you want.

How do I convert currency to string?

string value = (Convert. ToDecimal(ReturnValue)). ToString("c");


2 Answers

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.

like image 117
Lloyd Powell Avatar answered Sep 25 '22 03:09

Lloyd Powell


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

like image 20
Mohammed Swillam Avatar answered Sep 26 '22 03:09

Mohammed Swillam