Overflow Issue – Understanding Problems with Uint256 in Solidity

overflow

This is my first post here , I was not able to find the answer to my issue when I searched .
I am using Remix with Metamask ,
I created the contract and using the following code to mint .I have a minting limit in the contract . however the token is showing me the total supply as
a huge value "10000000000000000000" which is already higher than by total supply limit, because of which the check on minting stops the execution .

[![enter image description here][1]][1]

   function mint(uint noOfTokens) returns (bool) {
      checkTokens = _totalSupply.add(noOfTokens); 
   // Check the max limit and the owner before minting 
   require(_totalSupply <= maxLimit 
            && checkTokens <= maxLimit 
            && (msg.sender == address(owner)));
    balances[owner] = balances[owner].add(noOfTokens);
   _totalSupply        = _totalSupply.add(noOfTokens);
   return true;

}

The variables are defined as uint256.

Thanks for the help and support .
NKB.

[![enter image description here][2]][2]

pragma solidity ^0.4.11;

//ERC20 interface
import './ERC20.sol';
// safe math for over and under value attack.
import './SafeMath.sol';

contract Test is ERC20 {
// using safe math for uint256
using SafeMath for uint256;

uint256 public constant maxLimit = 500000000; 
uint256 public  _totalSupply = 0; 
string public constant symbol = "Test";
string public constant name = "TestCoin";
uint8 public constant decimals = 0;
// Rate -> 1 Ether = 1000 Just 
uint256 public RATE; 
// Ether reciever 
address public owner;
uint public start;
uint public startWeek;
uint public startTwoWeek;
uint public startFourWeek;
uint256 public tokens;
uint256 public checkTokens;

mapping(address=> uint256) balances; 
mapping(address=>mapping(address=>uint256)) allowed;


 // Fallback function
 // The function without name is the default function that is called 
 // whenever anyone sends funds to a contract

function() payable{
    createTokens();
}

function Test(){
   // balances[msg.sender] = _totalSupply;
owner  = msg.sender;  // get the owner of the token
start  = now;  // Get the current block time 
startWeek = start + 30 minutes ; // Get the scond block time
startTwoWeek = start + 60 minutes; // Get the third block time 
startFourWeek = start + 90 minutes; // Get the fourth block time . 

}

// Create new tokens
   function mint(uint noOfTokens) returns (bool) {
      checkTokens = _totalSupply.add(noOfTokens); 
   // Check the max limit and the owner before minting 
   require(_totalSupply/1000000000000000000 <= maxLimit 
            && checkTokens/1000000000000000000 <= maxLimit 
            && (msg.sender == address(owner)));
    balances[owner] = balances[owner].add(noOfTokens);
   _totalSupply     = _totalSupply.add(noOfTokens);
   return true;

}

 function createTokens() payable {
    require(msg.value >0 && _totalSupply <= maxLimit);

    if (now <= startWeek   && now > start) {
    RATE =    1150; 
    tokens = msg.value.mul(RATE);
    balances[msg.sender] = balances[msg.sender].add(tokens);
    _totalSupply = _totalSupply.add(tokens);
    owner.transfer(msg.value);
}

 if (now <= startTwoWeek && now > startWeek ) {
    RATE =    1100;
    tokens = msg.value.mul(RATE);
    balances[msg.sender] = balances[msg.sender].add(tokens);
    _totalSupply = _totalSupply.add(tokens);
    owner.transfer(msg.value);
 }

if (now <= startFourWeek && now > startTwoWeek ) {
    RATE =    1050;
    tokens = msg.value.mul(RATE);
    balances[msg.sender] = balances[msg.sender].add(tokens);
    _totalSupply = _totalSupply.add(tokens);
    owner.transfer(msg.value);
 }
 if (now > startFourWeek ) {
    RATE =    1000;
    tokens = msg.value.mul(RATE);
    balances[msg.sender] = balances[msg.sender].add(tokens);
    _totalSupply = _totalSupply.add(tokens);
    owner.transfer(msg.value);
 }


}
  function totalSupply() constant returns (uint totalSupply){
      return _totalSupply;
  }

  function balanceOf(address _owner) constant returns (uint balance){
      return balances[_owner];
  }

  function transfer(address _to, uint _value) returns (bool success){
      require( balances[msg.sender] >= _value &&
          _value > 0 );
          balances[msg.sender] = balances[msg.sender].sub(_value) ;
          balances[_to] = balances[_to].add(_value);
          Transfer(msg.sender,_to,_value);
          return true;
  }

  function transferFrom(address _from, address _to, uint _value) returns (bool success){
      require( allowed[_from][msg.sender] >= _value &&
      balances[_from] >= _value
      && _value > 0 );
    balances[_from]  = balances[_from].sub(_value);
    balances[_to]    = balances[_to].add(_value);
    allowed[_from][msg.sender]  = allowed[_from][msg.sender].sub(_value);
    Transfer(_from,_to,_value);
    return true;
  }

  function approve(address _spender, uint _value) returns (bool success){
   allowed[msg.sender][_spender] = _value;
   Approval(msg.sender,_spender,_value);
   return true;

  }

  function allowance(address _owner, address _spender) constant returns (uint remaining){
    return allowed [_owner][_spender];  
  }
  event GoalReached(address beneficiary, uint amountRaised);
  event Transfer(address indexed _from, address indexed _to, uint _value);
  event Approval(address indexed _owner, address indexed _spender, uint _value);

}

enter image description here
even though the token is defined with zero decimals it picks up 18 decimals for the wei and puts the value with all the decimals .

Best Answer

Because you have your decimals set to 18, you have to then divide by 1,000,000,000,000,000,000 (1 followed by 18 zeros) to get the integer-value of _totalSupply.

18 decimals is the standard for Ethereum. The smallest unit is referred to as a Wei. 1,000,000,000,000,000,000 wei are equal to 1 Ether. Here is a unit map.

To simplify your contract you could set the decimals to 0, in which case you would only be dealing with integer values that are NOT divisible. The downside is that you could NOT then offer fractions of a token.