#57012 - 05/09/19 01:48 PM
multiplication and divistion
|
OL Expert
Registered: 01/29/14
Posts: 400
Loc: Home
|
Hi, can someone help me with my math, it's not giving me the right results.
I simply need to multiply two numbers and divide them by another number.
For example: 51 * 7500 = 382500 then divide by 1000 = 382.5
&TotalBenefit := (mul(&myAge,7500)) &MaxValue := (Div(&TotalBenefit,1000))
I'm losing decimal point too! this is not helping me strtocur - floattostr
&MaxValue := (floattostr(Div(&TotalBenefit,1000))) ??? THANKS
Edited by Sami786 (05/09/19 02:11 PM)
_________________________
Peace
|
Top
|
|
|
|
#57014 - 05/09/19 03:37 PM
Re: multiplication and divistion
[Re: Sami786]
|
OL Expert
Registered: 01/29/14
Posts: 400
Loc: Home
|
can someone please help!
_________________________
Peace
|
Top
|
|
|
|
#57015 - 05/09/19 06:01 PM
Re: multiplication and divistion
[Re: Sami786]
|
OL Expert
Registered: 04/19/10
Posts: 316
Loc: London, UK
|
Hi, In order to preserve the maximum amount of accuracy in mathematical calculations, if you're working with decimal values to begin with, I'd always start with floating point numbers. So, firstly, let's make sure that your variables are type float. Helpfully, these are defined as measure in the variable declaration. If they're not measure/float, let's convert them to it. By type: &myFloatVar := strtofloat(&myStrVar) &myFloatVar := inttofloat(&myIntVar) &myFloatVar := curtofloat(&myCurVar) Then, we can do the calculations just as you would any other:
&myNewFloatVar := &myFloatVar * 7.5
NB: We don't need to divide by 1000 as 7500/1000 is 7.5Obviously if the number isn't going to be as clean as 1000 each time, then you can do it as another line:
&myNewFloatVar := &myNewFloatVar / 1000
Then if you need to present that value back to the original variable or to some other, just reverse the conversion steps above. So for string - for example:
&myStrVar := floattostr(&myNewFloatVar)
Hope this helps.
|
Top
|
|
|
|
#57022 - 05/13/19 03:58 PM
Re: multiplication and divistion
[Re: Sami786]
|
OL Expert
Registered: 01/29/14
Posts: 400
Loc: Home
|
Hi Jim, thanks for your help, I'm almost there just the last line of PPTalk code does not work for me!
define(&myAge,integer,0) set(&myAge,strtoint(@(1,1,2))) define(&myIndex,integer,0)
define(&PASbenefit,measure,0) &PASbenefit := strtofloat(@(3,1,8))
define(&arrAge,arrayinteger,[50,51,52]) define(&arr1stYearVPUmale,arraystring,['63','67','71']) define(&arr1stYearVPUFemale,arraystring,['39','42','44'])
define(&Value1,string,'0') define(&i,integer,0) define(&Gender,string,'0')
for(&i,0,1,2) if(&arrAge[&i] = &myAge) &myIndex := &i endif() endfor()
&Gender := @(2,1,1)
if(&Gender = 'M') &Value1 := &arr1stYearVPUmale[&myIndex] else &Value1 := &arr1stYearVPUFemale[&myIndex] endif()
&FirstYearVPU := &Value1
define(&myFloatVar,measure,0) &myFloatVar := strtofloat(&FirstYearVPU)
define(&Value2,measure,0) &Value2 := (&myFloatVar * &PASbenefit)
define(&TotalValue,measure,0) &TotalValue := &Value2 /1000
%&TotalBenefit := strtofloat(&TotalValue)
Note: Global variable &TotalBenefit is assigned as string! can you help.
THANKS
Edited by Sami786 (05/13/19 04:00 PM)
_________________________
Peace
|
Top
|
|
|
|
#57023 - 05/13/19 05:34 PM
Re: multiplication and divistion
[Re: Sami786]
|
OL Expert
Registered: 01/29/14
Posts: 400
Loc: Home
|
when I change it to below I get Blank !
&TotalBenefit := &TotalValue
any idea why?
_________________________
Peace
|
Top
|
|
|
|
#57025 - 05/14/19 10:44 AM
Re: multiplication and divistion
[Re: Sami786]
|
OL Expert
Registered: 01/29/14
Posts: 400
Loc: Home
|
can someone help here please?
_________________________
Peace
|
Top
|
|
|
|
#57026 - 05/14/19 10:56 AM
Re: multiplication and divistion
[Re: Sami786]
|
OL Toddler
Registered: 04/18/18
Posts: 50
|
Hi Sami786,
Likely your string contains characters that cannot be converted to float, such as the comma separating thousands. Make sure you only have digits and periods in the string you pass to the strtofloat().
|
Top
|
|
|
|
#57027 - 05/14/19 11:13 AM
Re: multiplication and divistion
[Re: Sami786]
|
OL Expert
Registered: 04/19/10
Posts: 316
Loc: London, UK
|
Strip the variable of those values. I also notice your last line is commented.
|
Top
|
|
|
|
#57028 - 05/14/19 11:21 AM
Re: multiplication and divistion
[Re: Sami786]
|
OL Expert
Registered: 01/29/14
Posts: 400
Loc: Home
|
I striped the comma but it's still not working
define(&stripcomma,string,'0') &stripcomma := strip(',',@(3,1,8))
_________________________
Peace
|
Top
|
|
|
|
#57029 - 05/14/19 11:34 AM
Re: multiplication and divistion
[Re: Sami786]
|
OL Expert
Registered: 01/29/14
Posts: 400
Loc: Home
|
strip the comma helped, but I don't get the zero at the end, 74.8 I need to get 74.80
_________________________
Peace
|
Top
|
|
|
|
|
|