# How to do division and multiplication using solidity?

I am building a program on solidity that receives 3 values(bonus, value1, value2) through function, Then, another value called priority is calculated as follows: `Priority= (0.5*Value1 + 0.5*value2) / bonus`. However, when I code this function in remix, I got errors. Is there any way to fix this and I still can compute the "Priority" on solidity?

``````pragma solidity ^0.4.24;
contract Test{

struct student{
uint bonus;
uint value1;
uint value2;
uint Priority;  //priority of student
}

mapping(address => student) students ; //etherum does not allow
declaration of a function of type mapping so we define array of addresses

_value2) public {

var student = students[_address]; //instance of struct student and map it to
students mapping
student.bonus=_bonus;
student.value1=_value1;
student.value2=_value2;
student.Priority=(0.5 * _value1 + 0.5 * _value2)/ _bonus;

}

function get_ESU(address ins) view public returns (uint, uint, uint) {
return (students[ins].value1, students[ins].value2,
students[ins].Priority);
}

}
``````
• what kind of issues do you have? Oct 3 '18 at 17:05

The problem is simply that ‘0.5’ do not exist in integer math. The minimum positive number is ‘1’.

You can divide by 2 instead of multiplying by 0.5.

Here's an explained code example that should get you on the right track.

``````pragma solidity ^0.4.24;

contract Test {

// storage is packed based on how much data we actually want to store,
// to minimise the storage costs, we're going to use expected uint types
struct StudentType {
uint8 bonus;        // 0 - 255
uint16 value1;      // 0 - 65535
uint16 value2;      // 0 - 65535
uint16 Priority;    // 0 - 65535
}

mapping (uint256 => address) public studentsByInsertId;
uint256 public studentCount = 0;

// instance of struct student and map it to students mapping;
student.bonus = _bonus;
student.value1 = _value1;
student.value2 = _value2;

student.Priority = this.calculatePriority( _value1, _value2, _bonus);

// map address to current insert id
// ++uint makes our index start at 1 since the incrementation happens before
// if you want it to start at 0, then do uint++

}

/*
solidity does not support floating point math, so we can't really use "decimals"
and to go around this, we multiply everything we need by 100, and divide the result by 100
keep in mind division also floors the result ( in case the result 0 to 0,999(9) it will be 0 )

depending on the precision you actually want the more "zeroes" you're going to use.
*/

function calculatePriority(uint16 _value1, uint16 _value2, uint8 _bonus) pure public returns ( uint16 ) {
// store the calculation in a big enough value
uint256 result = ( ( ( 50 * _value1 ) + ( 50 * _value2 ) ) / _bonus ) / 100;

// convert result
return uint16( result );
}

/*
not really needed since you can use the "studentsByAddress" mapping method created by solidity
unless you want to "hide" or manipulate some data
*/
function get_ESU(address ins) view public returns (uint, uint, uint) {
return (