I am trying to accept a decimal number as input inside solidity function but I am unable to do so.
I know decimal number doesn't work in solidity but is there any way to accept it?
No, there is no decimal type in solidity, but you can scale your numbers (for instance multiply numbers in 10^3) and treat them as int in your contract.
Related
I am trying to do the following calculation with solidity:
3,000 / 45,000,000 = 0.000067 with the following method:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
function divide(uint _num1, uint _num2) public pure returns(uint _result) {
return (_num1 /_num2 );
}
But obviously, I am getting the results as zero.
I am aware that solidity for transactions uses 18 decimal points (using Wei), however, I could not find in the replay to all the previous questions regarding the decimals in solidity how to do it for regular numbers, or regular calculations. Also most of the time it does not even work with transactions.
What is the actual commonly used standard practice method to do "ANY" division from "ANY" number, regardless being for Ether, Tokens, or regular calculations?
I mean a method that would work for 3 / 100 just as well as 2.5 / 3.7?
Floating-point arithmetics are not supported in Solidity however you can use fixed-point arithmetics considering that you have 18 points of decimals if you use uint256.
How can I multiply a uint by a decimal fraction integer? This is what I have so far - I am explicitly converting the decimal fraction integer to type uint. Will this have unintended consequences?
uint gasPriceInWei = tx.gasprice * 1000000000;
uint percentOfGasPrice = uint(transactionCostInWei) * uint(.1);
Solidity does not support floating point numbers (yet). You need to store as an integer and convert on the client side. See the note here.
To learn a bit about ethereum, I thought I'd code up a simple loan contract. I added a few simple properties for any loan, and I immediately ran into problems.
contract Loan
{
address lender;
address borrower;
uint amount;
???? interestRate;
...
}
What type do I use for the interest rate? Looking at the solidity types documentation, the primitive types include Boolean and multiple kinds of integers. There is no primitive for decimal numbers.
Without decimal numbers, how do I calculate the interest?
You can use another unit.
Like multiply every percentage by 100, you achieve a 2 number after comma precision
1% => 100
0,1% => 10
0,01% => 1
If it's not enough, you can use other multipliers.
The Solidity changelog shows that fixed point types will be included in version 0.3.0 which is in development.
The val() function seems to understand . as a decimal point, even on systems with , as a decimal point. For example, the following call
val("7,3") + 1,4
returns the real number 8,4 (8.4 in English notation). val("7.3") + 1,4 returns the expected value 8,7.
Is this a feature or a bug? How can I specify which decimal point will be used in function val()?
According to the documentation:
Note The Val function recognizes only the period (.) as a valid
decimal separator. When different decimal separators are used, as in
international applications, use CDbl instead to convert a string to a
number.
Note: Val(7,3)+1,4 also returns 8,4, so be careful if your input already is in the number format!
I defined (through PHPMYADMIN) a field with the float data type.
When I try to insert a number as 78.556677099932222377 it will translate it to 79.
How do I make it to save the correct number, at least 7 places after the decimal dot?
Open phpMyAdmin with structure option and define the float as:
FLOAT(23,19)
clear in this picture:
How do you define the float in phpMyAdmin
FLOAT(23,19)
when u declare the field you could use that above.
Better to go with Cygnusx1 and change to decimal
see MySQL numeric types
and also Problems with Float
DECIMAL is what you looking for.
ref:
SQL Decimal is used to specify the values in decimal datatype. The Decimal datatype can be of two types : Decimal (p) refers to floating point and Decimal fixed point (p,s). The DECIMAL data type can store decimal floating-point numbers up to a maximum of 32 significant digits and Decimal fixed point can store upto 16 significant digits.