# multiplication and divistion

Posted by: Sami786

## multiplication and divistion - 05/09/19 01:48 PM

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
Posted by: Sami786

## Re: multiplication and divistion - 05/09/19 03:37 PM

Posted by: jim3108

## Re: multiplication and divistion - 05/09/19 06:01 PM

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:
Code:
`&myFloatVar := strtofloat(&myStrVar)`

Code:
`&myFloatVar := inttofloat(&myIntVar)`

Code:
`&myFloatVar := curtofloat(&myCurVar)`

Then, we can do the calculations just as you would any other:
Code:
```&myNewFloatVar := &myFloatVar * 7.5
```

NB: We don't need to divide by 1000 as 7500/1000 is 7.5

Obviously if the number isn't going to be as clean as 1000 each time, then you can do it as another line:
Code:
```&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:
Code:
```&myStrVar := floattostr(&myNewFloatVar)
```

Hope this helps.
Posted by: Sami786

## Re: multiplication and divistion - 05/13/19 03:58 PM

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

Posted by: Sami786

## Re: multiplication and divistion - 05/13/19 05:34 PM

when I change it to below I get Blank !

&TotalBenefit := &TotalValue

any idea why?
Posted by: Sami786

## Re: multiplication and divistion - 05/14/19 10:44 AM

Posted by: jouberto

## Re: multiplication and divistion - 05/14/19 10:56 AM

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().
Posted by: jim3108

## Re: multiplication and divistion - 05/14/19 11:13 AM

Strip the variable of those values.

Code:
`&var := strip(‘,’, &var)`

I also notice your last line is commented.
Posted by: Sami786

## Re: multiplication and divistion - 05/14/19 11:21 AM

I striped the comma but it's still not working

define(&stripcomma,string,'0')
&stripcomma := strip(',',@(3,1,8))
Posted by: Sami786

## Re: multiplication and divistion - 05/14/19 11:34 AM

strip the comma helped, but I don't get the zero at the end,
74.8 I need to get 74.80
Posted by: jouberto

## Re: multiplication and divistion - 05/14/19 11:43 AM

Hi Sami,

Just specify the amount of decimals required as an additional parameter for your strtofloat(stringtoconvert,2). the (***,2) indicates you want 2 decimals.

This is all documented within the online help, you might want to bookmark the presstalk reference guide if you need to work your way through expressions.

hope this helps. Thanks.
Regards,
Posted by: Sami786

## Re: multiplication and divistion - 05/14/19 01:35 PM

THANK YOU
Posted by: Sami786

## Re: multiplication and divistion - 05/14/19 04:04 PM

fixed thank you all for you help
Posted by: Sami786

## Re: multiplication and divistion - 05/14/19 06:00 PM

this helped thank you for all your helps. I'm good now.

&TotalBenefit := floattostr(&TotalValue,2)