Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't this string format as currency?

I have the following line:

//Send Email
clntMailBody = clntMailBody + "Order Total: " + String.Format("{0:C}", strOrderTotal + "\n");

Watch shows:

String.Format("{0:C}", strOrderTotal + "\n")    "35\n"  string

But it only outputs "35". I expected "$35.00" Why is this not working as intended?

Thanks

like image 308
Kolten Avatar asked Apr 25 '12 21:04

Kolten


2 Answers

I'm guessing strOrderTotal is a string? I think {0:C} only works for decimal or int types.

like image 193
SouthShoreAK Avatar answered Oct 08 '22 19:10

SouthShoreAK


I can't believe all of these answers and no one mentioned this, change your code to

clntMailBody = clntMailBody + "Order Total: " + String.Format("{0:C}", strOrderTotal) + "\n";

And see if that solves your problem, however a better way to do it would be

clntMailBody = String.Format("{0}Order Total: {1:C}\n", clntMailBody, strOrderTotal);

It is much easier to see what is going on and removes a lot of your string concatenation.

If you are willing to do some more re-writing a even better solution is: (I made some logic up to show my example)

StringBuilder clntMailBody = new StringBuilder();

clntMailBody.AppendLine("Some Fixed body Text")

foreach(string lineItem in Invoice)
{
    clntMailBody.AppendLine(lineItem);
}

clntMailBody.AppendFormat("Order Total {0:C}", strOrderTotal).AppendLine();

return clntMailBody.ToString();
like image 39
Scott Chamberlain Avatar answered Oct 08 '22 20:10

Scott Chamberlain