I have this code in my contract:

require(msg.value == 0.06 ether);

But I want to replace 0.06 with a variable.
So I tried this:

uint256 price = 0.06;
require(msg.value == price ether);

But this doesn't work.

So does anyone know how to combine variables with an Ether unit suffix?


You can do this:

uint256 price = 0.06 ether;
require(msg.value == price);

You can't do uint256 price = 0.06 because uint256 is an integer type.

You could do something like this, I suppose:

uint256 percentage = 6;
require(msg.value == 1 ether * percentage / 100);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.