Author

Topic: Checking inputed numbers are permissible bitcoin number types (Read 752 times)

legendary
Activity: 1246
Merit: 1077
I am working in PHP and I want to check if a given user submitted input is not too big or too small, where the input represents an amount in Bitcoins (don't worry I am sanitising the data and not blindly using it).

1. I am storing the amounts in the database as 64 bit integers (multiply them by 10^8 to avoid rounding errors in future calculations). I am limiting the amounts so that they cannot exceed the following precision: no more than 4 numbers after the decimal point. Also since no-one can have more than 100 million bitcoins, I also want to specify no more than 8 numbers precending the decimal point. How can I achieve this neatly?

My current approach seems a bit hack-ish:

Code:
//Check for existence of decimal point in string using strpos

//explode the string by the decimal point

//do a strlen on both the strings and check they dont exceed 8 and 4 respectively

//if no decimal point, simply do a strelen and check it's not greater than 8


2. Also, I don't want the inputted data to be smaller than 0.0001. I am proficient in php for web design not familiar with the math functions of php is there an easy way to handle this?

Thanks for any tips  Smiley

1&2. Convert the string to float, multiply by 100000000, round, and then take modulo with 10000 or check size. Code below:

NB: I don't speak PHP, so this is JavaScript with whatever limited PHP I know. There might be problems with it.

Code:

$fStr 
1e8 floatval($str);
$iStr round($fStr);
if (
$iStr 10000 != 0) {
  
// Too many decimal points
} else if ($iStr 1e16) {
  
// Too big
} else if ($iStr 1e4) {
  
// Too small
}

?>


Warning: You should probably limit the values below 100 million. It is possible for floating-point errors to take over at around that number, and results may be a satoshi off.
legendary
Activity: 1344
Merit: 1001
I am working in PHP and I want to check if a given user submitted input is not too big or too small, where the input represents an amount in Bitcoins (don't worry I am sanitising the data and not blindly using it).

1. I am storing the amounts in the database as 64 bit integers (multiply them by 10^8 to avoid rounding errors in future calculations). I am limiting the amounts so that they cannot exceed the following precision: no more than 4 numbers after the decimal point. Also since no-one can have more than 100 million bitcoins, I also want to specify no more than 8 numbers precending the decimal point. How can I achieve this neatly?

My current approach seems a bit hack-ish:

Code:
//Check for existence of decimal point in string using strpos

//explode the string by the decimal point

//do a strlen on both the strings and check they dont exceed 8 and 4 respectively

//if no decimal point, simply do a strelen and check it's not greater than 8


2. Also, I don't want the inputted data to be smaller than 0.0001. I am proficient in php for web design not familiar with the math functions of php is there an easy way to handle this?

Thanks for any tips  Smiley
Jump to: