I've tried

```contract OToken {

using SafeMath for uint256;

uint public _totalSupply = 0;
uint public constant _cap = 100000000;
string public constant symbol = "OXN";
string public constant name = "OToken";
uint public constant decimals = 18;

uint public oneTokenInWei = 183.602;
```

If I want the token price to be \$0.02 each and 1 eth is trading at \$167 then 1 wei = 183.602 tokens

I can call this function if I want to change the price per token to .03

``` function setOneTokenInWei(uint w) onlyOwner {
oneTokenInWei = w;
changed(msg.sender);
}
```

then this function to create the token

```function createTokens() payable{

require(
msg.value > 0
&& _totalSupply < _cap
&& CROWDSALE_PAUSED <1
);

uint multiplier = 10 ** decimals;
uint256 tokens = msg.value.mul(multiplier) / oneTokenInWei;

owner.transfer(msg.value);
}
```

this is not adding the current value to the senders wallet

Sep 25, 2018 8,158 views

## 1 answer to this question.

We know that 1 Ether = 1018 wei, if we take that 1 Ether = \$ 167

Then for \$ 0.02 we should get \$ 0.02 / \$ 167 = 0.00011976047904191617 Ethers.

This equals to 1018 x 0.00011976047904191617 = 119760479041916.17 wei.

So if you put 1 Ether you will get 1018 / 119760479041916.17 = 8350 tokens.

At the price of \$ 167 each token value is 167 / 8350 = 0.02 as desired.

I'd define a function so you can set the price of one ether and it derive the price of one token at two cents.

```function setEthPrice(uint _etherPrice) onlyOwner {
oneTokenInWei = 1 ether * 2 / _etherPrice / 100;
changed(msg.sender);
}```
• 29,350 points
is the _etherPrice agrument's price in the ether or the dollar ??

Hi, @There,

Ethereum is an alternative digital currency to bitcoin and had been trading as high as \$352 on Wednesday. It has since rebounded from its flash-crash lows to trade to about \$325 on the GDAX exchange. According to industry and price tracking website Coinmarketcap, which takes into account the price on several exchanges, ethereum was trading around \$338.

## How to set the ERC20 token price in respect to ether in solidity?

Hi, @Jas, I would suggest you go through ...READ MORE

+1 vote

## How to convert INT to STRING in Solidity?

Look at the following code : function uintToString(uint ...READ MORE

## How to set web3 solidity variable value?

The setLog transaction has not yet been mined by ...READ MORE

## Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

## Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

## Solidity geth: Error encountered during contract execution [Bad instruction]

recipes is a dynamic storage array. You need ...READ MORE