-3

I have a string filed which holding the dollar amount. What I want to do is process the string and covert in actual currency.

Example:

Possible incoming string;

80
1000.00
1000.0
-100

Desired Outputs are;

100.00
1,000.00 
-100.00

How can I format the string with regex and covert it to the output I want?

oɔɯǝɹ
  • 7,219
  • 7
  • 58
  • 69
Ramsey K
  • 23
  • 8

2 Answers2

2

It's not really clear what you mean by actual currency, but I'm assuming you just want a string that looks like the samples you've posted. Keep in mind that the decimal type (the type best suited to represent currency) doesn't have any actual formatting information.

You'll have to parse the string using decimal.Parse, then convert the value back to a string to get it into the desired format.

For example:

public string Format(string input)
{
    decimal value = decimal.Parse(input);

    return value.ToString("#,#.00");
}

// usage: Format("1000.0")

Example: https://dotnetfiddle.net/P7psPC

However if you're dealing with currency you can just use the C format specifier:

value.ToString("C");

This would output the following for your sample inputs:

$80.00
$1,000.00
$1,000.00
($100.00)
Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
  • Yes correct I want the output looks like the sample I gave above. But what about the .00 at the end what if string comes 189.6 would that code convert this to 189.60 and string also might be negative and I want this to look like -189.60 – Ramsey K Oct 06 '14 at 20:29
  • @Ramsey K: Yes, those requirements are met with the function I posted. I added an example you can play with. – Andrew Whitaker Oct 06 '14 at 20:31
  • @AndrewWhitaker I clarified your `ToString()` in my answer, but tried to not repeat or overstep your question. Good answer. – Greg Oct 06 '14 at 20:51
0

There isn't a real currency converter in Microsoft .Net Framework, so you have to work within the constraints provided, which is decimal. The closest you can get to your decimal being defaulted to money would be:

decimal amount = 0.00m

The m at the end is declaring that this decimal will represent money. Which when added will provide basic rounding as if it were money.

However the easiest approach would be how Andrew Whitaker did the conversion. The only thing I would add, is that when you utilize:

value.ToString("#,##0.00"); // Output: 0.96
value.ToString("#,#.00");   // Output .96
value.ToString("0.##");     // Output 0.6

To clarify the conversion with ToString(); for the decimal.

Greg
  • 11,302
  • 2
  • 48
  • 79
  • Thanks Greg I will keep these in mind. Appreciated – Ramsey K Oct 06 '14 at 21:06
  • @Andrew Whitaker, Hi Andrew hope you don't mind me asking another question. How can I add/insert double quote at the beginning and end of the string. Such as -1,000.00 I want this to look like "-1,000.00" Thanks – Ramsey K Oct 07 '14 at 16:32
  • I'm not Andrew, but the easiest way would be concatenation: `string content = @"/"" + Format(value) + @"/"";` That would be the easiest, you'll need to use the `/` to escape it. – Greg Oct 07 '14 at 16:35
  • Thank you :) Is this going to look something like that? I need to convert this to inline C# code : public string AddQuote(string input) { string content = @"/"" + Format(input) + @"/""; return content; } – Ramsey K Oct 07 '14 at 16:49