Forum Discussion
Altera_Forum
Honored Contributor
15 years agoYou can also use the modulus operator - %. Here is how you can implement it:
If you take the number 45 and divide it by 2 with the division operator: 45/2, the result will be only the whole part 22, right. After that use the modulus operator with the number 45: 45%2, the result will be 1. If you want to have only one digit after the decimals multiply the remaining part by 10 and than divide it again by 2: 1*10; 10/2 = 5. You should have two separate register for the whole part and the remaining part. Let's do another example to make things clear: If you have 41/7 and you want two digits after the decimals:
reg number1 = 8'd41;
reg number2 = 8'd7;
reg whole_part = 8'd0;
reg remain_part = 8'd0;
always@(negedge key3) begin
whole_part = number1 / number22;
remain_part = ((number1 % number2) * 100) / number2;
end
This will return whole_part = 5 and remain_part = 85. Best regards, VT