# How should I multiply ether by a decimal?

I'd like to multiply Ether by a decimal (a proportion, e.g. 0.05). I've got:

ufixed proportion; // What type should proportion be?

function functionName() {
var Ether = msg.value; // (in units of wei)
var proportionOfEther = Ether * proportion
...

My Solidity linter is saying that * is not compatible with uint256 (the Ether) and ufixed128x128 (the proportion).

• msg.value is in Wei not Ether. It should be quite a big number, because Wei are very small. Life is easier if you just avoid decimals, eg store your proportion as percent, divide by 100 and multiply by it. Jan 30 '17 at 23:39
• See ethereum.stackexchange.com/questions/11733/… for an example Jan 31 '17 at 0:09

Instead of multiplying by a decimal, multiply and divide by integers.

i.e. (Ether * 5)/100

• This is exactly what you should do, but be certain to use the parens to do it in the order shown. If you divide first, I think you would get a different result due to truncation. Also, if I were you, I would be careful to think in terms of wei, not ether. The assignment var Ether = msg.value is already a bit off (at least the name of the variable is) because msg.value is in wei. You comments says that, but the variable name disagrees. A minor point, I know, but when dealing with this stuff, you have to be super careful in my experience. Jan 31 '17 at 0:02
• ^This is important. Whenever possible, avoid using Ether as a unit. It is far easier to reason about an integer quantity of Wei than a fractional quantity of ether. Jan 31 '17 at 0:05
• @ThomasJayRush TjadenHess Thank you, I have changed the variable names to wei. I was using Ether to refer to the currency, not the denomination, but that isn't important. Jan 31 '17 at 11:50
• also you should note that the namesether, wei, finney, etc. are reserved in Solidity. (ether == 10**18, finney == 10**15, etc.) Jan 31 '17 at 16:12
• If I have 7 Ether, then the result will be 0.35. At any rate, it will get chopped off as a zero. Jun 15 '17 at 8:01

You can use DS-Math (I answered that here in more detail)

fixed point math does not work in solidity yet.

That is why you need to use the methods listed above.

You may use mulu function from ABDK Math 64.64 library. It multiplies uint value by 64.64-bit fixed point number.