I am not sure how to accept a decimal input to the function then calculate with token decimal.

Said the token decimal is 18 If the function wants to accept the value 123.4567, how to get the output result in 123456700000000000

I tried the following won't work because the input type already cut the decimal before it entered into the function

function chkAmt(uint _amt) view public returns(uint256){
     uint256 finalAmt = amt;
     return finalAmt;

This output 123 without the remaining number. I think the uint _amt already ignore the decimal as it is unsigned integer.

I tried the following also failed. If I input with 123.34567

uint8 public token_decimal = 18;
function chkAmt(uint _amt) view public returns(uint256){
    uint256 decfactor = 10**uint256(token_decimal);
    uint256 rtnamt = _amt.mul(decfactor);
    return rtnamt;

The output is 123000000000000000000 which is not right.

What is the correct way to input 123.4567 and get the output to 123456700000000000000???


Actually, all internal and function interfaces deal in integers. The token_decimal value is part of the ERC20 standard. It is a signal to user interfaces that values should be divided by 10**18 to make human-readable representations. For example, a dollar would be 100 / decimals 2.

You will get the best results by externalizing that concern to clients. In other words, force clients to speak the contract's language and do not implement any form of conversion inside the contract. In my opinion, addressing that concern introduces unwanted and unnecessary complexity in most contracts, so it should be avoided.

Hope it helps.

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.