When using .ToString("{blah}") it gives an error because it's a ?double not a double... 'bad parameter' etc
Note this does not seem to be working, sometimes I get '5.7':
double itemListPrice = Math.Round((double)((item.UserIntervalPrice * item.Volume) -
((item.UserIntervalPrice * item.Volume) * (item.VolumeDiscount / 100))),2);
htmlReceipt += "<tr><td>" + item.Title + "</td><td>" + item.Description + "</td><td>" +
item.Volume + "</td><td>$" + itemListPrice.ToString() + "</td></tr>";
Have you tried:
double? myDouble = 10.5;
if (myDouble.HasValue)
{
string currency = myDouble.Value.ToString("c");
}
I find the following to work quite well:
double? val1 = null;
double? val2 = 5.7d;
var s1 = string.Format("{0:c}", val1); // outputs: ""
var s2 = string.Format("{0:c}", val2); // outputs: $5.70
I wouldn't worry too much about performance in this case, and be more concerned with correctness and clarity, myself.
I would also suggest you consider using string.Format()
or a StringBuilder
in lieu of concatenating string fragments individually. Not this this is a huge deal, but it does allocate and discard intermediate strings unnecessarily; which, if you're concerned about performance you'd probably want to eliminate; for example:
htmlReceipt +=
string.Format( "<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3:c}</td></tr>",
item.Title, item.Description, item.Volume, item.listPrice );
使用小数来处理货币值,并使用Decimal.ToString(“ C”)进行格式化。
好吧,看起来像这样工作:
((double)theVariable).ToString("c");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.