Announcements
Hi there everyone,
In calculating the "_totalcost" in the last line I'm having problem with casting decimal to currency and getting this error:
"input string was not in correct format."
Any Idea how to cast these two
decimal? _totalweight = 0; Money _unitprice ; Money _totalcost ; _unitprice = _product.sfd_unitprice; _unitprice =new Money(1); _totalcost = _opportunityproduct.sfd_totalcost; if (_totalcost==null) { _totalcost = new Money(0); } _totalcost = new Money(decimal.Parse(_totalweight.ToString()) * decimal.Parse(_unitprice.ToString()));
*This post is locked for comments
As suggested u cannot directly multiply money with decimal value so u need to convert it to one type i.e. first decimal and then multiply it get the value n then finally convert it to Money. That's it.
Money type accept a decimal as an input value.
Basically all you need is :
Money totalCost = new Money(myDecimalValue);
If you need to calculate with decimals, you just make the calculation and have the results as a decimal, and then set the result to the Money:
decimal totalCost = cost1 * cost2;
Money totalMoneyCost = new Money(totalCost);
Hope this helps.
Hi z.sam,
when you have Money values you need to convert it to decimal values, deal with decimal values and finally reconvert them to Money, some examples follows:
// you need to deal with decimal values ... decimal _totalweight_decimal = 0m; decimal _totalcost_decimal = 0m; decimal _unitprice_decimal = 1m; // .. then convert to money _totalcost_decimal = _totalweight_decimal * _unitprice_decimal; _totalcost = new Money(_totalcost_decimal); // to get decimal value from money value, early bound decimal moneyattributevalue_decimal = 0m; if (myentity.moneyattributevalue != null) { moneyattributevalue_decimal = myentity.moneyattributevalue.Value; } // to get decimal value from money value, late bound decimal moneyattributevalue_decimal = 0m; if (moneyattributevalue.Attributes.Contains("moneyattributevalue")) { moneyattributevalue_decimal = myentity.GetAttributeValue<Money>("moneyattributevalue").Value; }
Please let me know if you solve.
If you found the answer helpful, please mark as Verified
Join my network on LinkedIn Follow me on Twitter
Thank You & Best Regards
Francesco Picchi
Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY
Independent Contractor
André Arnaud de Cal...
294,118
Super User 2025 Season 1
Martin Dráb
232,866
Most Valuable Professional
nmaenpaa
101,158
Moderator