So I am having difficulty with multiplying in Wei.

======================================================================= Here is the short version:

I want to multiply 7000 ether by 1.25 to get 8750 ether as the expected result.


Here is the long confusion version:

I have a Smart Contract that pulls the USD value of ETH then performs a conversion of the USD value at 75 cents on the dollar an issues the tokens appropriately.

My issue is multiplying in Wei.

I would have loved to do all the calculations in Wei but I need to have a USD conversion as part of the formula.

I do want to continue using SafeMath in the calculations.

Say I have 7 ETH sent to the contract The ETH price is 1,000 USD at the time Which makes the USD value at 7,000 The exchange rate is 0.001 USD to ETH

The tokens are issued at .75 to 1 dollar so I multiply by 1.25 to get the 8,750 tokens as the final number in regular math

I have all of it except multiplying by 1.25

I assumed I would multiply by 1250000000000000000 Wei for 1.25 but my final number it way to large.

Whats the right way to perform that math in solidity?

Here is my code:

pragma solidity ^0.4.18;

import "./SafeMath.sol";
import "./Ownable.sol";
import "./usingOraclize.sol";

contract Test is Ownable, usingOraclize {

  using SafeMath for uint256;

  uint public ETHUSDPriceUINT = 1000000000000000000; // this is a place holder for the Oraclize API call back variable

  function Test(address _tokenAddress, address _multiSigAddress) {


  function buyTokens(string _ETHprice) payable returns (uint) {
      result = _ETHprice; // provide the current price of ETH in USD
      uint ETHContributionInWei = msg.value;
      ETHUSDPriceUINT = parseInt(result, 2); // let's use two decimal points
      uint exchangeRate = ETHUSDPriceUINT.div(ETHUSDPriceUINT);
      uint USD_Value = ETHContributionInWei.mul(ETHUSDPriceUINT);
      uint tokenFactor = 1250000000000000000 wei;
      uint numTokensIssued = USD_Value.mul(tokenFactor);

      return numTokensIssued;


  function () payable {



I want to multiply 7000 ether by 1.25 to get 8750 ether as the expected result.

function doit() public pure returns (uint256) {
    return 7000 ether * 125 / 100;
| improve this answer | |

I think this is what you're looking for

function convert() payable public returns (uint) {
  uint exchange = 1000;

  return ((msg.value * exchange) * 5 / 4) / (1 ether);
| improve this answer | |

This is how I typically handle the conversion of USD -> Ether -> tokens.

The below is not function Solidity code - but segments of code

uint256 USD_CENT_PER_TOKEN // USD in cents as a whole integer so .50 cents would be 50
uint256 _usdPerEth // the usdPerEth rate incoming as a constructor parameter

// set via the constructor for the rate variable (assuing an Open Zeppelin based crowdsale)
// .mul(1e2) is used to convert whole dollars into cents - not required if value is already in cents

// used in the buy method
uint256 tokenAmount = msg.value.mul(rate);
| improve this answer | |

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.