Bitcoin Forum
May 25, 2024, 03:51:40 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Checking inputed numbers are permissible bitcoin number types  (Read 736 times)
mr_random (OP)
Legendary
*
Offline Offline

Activity: 1302
Merit: 1001


View Profile
May 12, 2013, 07:43:35 PM
 #1

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
dree12
Legendary
*
Offline Offline

Activity: 1246
Merit: 1077



View Profile
May 12, 2013, 08:06:12 PM
 #2

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:
<?php

$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.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!