• Status: Solved
• Priority: Medium
• Security: Public
• Views: 904

# How to multiply COleCurrency with float?

I would like to multiply a COleCurrency object with a float object and have the result as COleCurrency. What I've did is,

COleCurrency loanAmount(100,0);
float interest = 6.75;
COleCurrency totalLoanAmount = loanAmount + (loanAmount * interest / 100);

I got the warning "conversion from 'float' to 'long', possible loss of data"  when I compile. True enough, the result I obtained was multiplication with 6 and not 6.75.

Would like help on the correct method to do it.

Thank you.

Eg.,

COleCurrency loanAmount = 1000.00;
float interest = 6.79;

0
rayocs
• 4
• 2
1 Solution

Commented:
Why you cant use ColeCurrency instead of float ?

like :

COleCurrency loanAmount(100,0);
COleCurrency interest(6,75);
COleCurrency totalLoanAmount(00,00);

totalLoanAmount = loanAmount + (loanAmount * interest / 100);

-MAHESH
0

Commented:
it seems COleCurrency not support * + operators..

you may try converting that to float using atof :

COleCurrency loanAmount(100,0);
float interest = 6.75;
float  totalLoanAmount = (float)atof(loanAmount.Format()) + (float)(atof(loanAmount.Format())  * interest / 100);

-MAHESH
0

Author Commented:
Yes, I might be able to do that. However, I need the final result to be a COleCurrency object. How can I do the conversion? Ie. from float to COleCurrency.
0

Commented:
>>I need the final result to be a COleCurrency object

Check if you can set using COleCurrency::SetCurrency().. you may use modf() function from math.h to separate integer and fraction parts.

-MAHESH
0

Commented:
e.g. this should work :

COleCurrency loanAmount(100,0);
float interest = 6.75;
float  totalLoanAmount = (float)atof(loanAmount.Format()) + (float)(atof(loanAmount.Format())  * interest / 100);

COleCurrency amtLoan;
double ipart;
double fract=modf(totalLoanAmount,&ipart);

amtLoan.SetCurrency((long)ipart,(long)(fract*1000));

you need to include math.h in order to use modf().

-MAHESH
0

Author Commented:
It works well. Thanks Mahesh.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.