web
You’re offline. This is a read only version of the page.
close
Skip to main content
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

I have the same question (0)
  • 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

  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 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.

  • 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.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Community Member Profile Picture

Community Member 2

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans