Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Casting decimal to currency

Posted on by 299

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

  • Suggested answer
    Sid07 Profile Picture
    Sid07 285 on at
    RE: Casting decimal to currency

    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.

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Casting decimal to currency

    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.

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Casting decimal to currency

    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

    http://www.francescopicchi.com

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans