Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Casting decimal to currency

(0) ShareShare
ReportReport
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
    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
    30,188 Moderator 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
    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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,118 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,866 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans