I am working on this affiliate that computes numbers and adds them up then i get a number like 5.1882449992845E+25 how do i force the number to 2 decimal places so that i can have just integer.
I would like to know any php function that this with such numbers and what it means.
Use number_format php function:
number_format(5.211545645612456,2);
For more information refer http://php.net/manual/en/function.number-format.php
The number 5.1882449992845E+25 really means 5.1882449992845 * 10^25 (see here).
Unfortunately this is massive. If you are intent on storing this as a number (i.e. if you need to apply maths to it) then you should be able to parse the string and apply the exponent. Be warned however the max int size in PHP is usually 2 Billion (although this is platform independent). It's unlikely you'll be able to store this as an int. Otherwise if it is just to be used as an id or the like I would leave it as is or alternatively convert it to an int string.
Related
I'm new to math in PHP. I'm working on calculating cryptocurrencies that have up to 18 decimal places. What is the best way to calculate this precisely in PHP? Most answers on SO are for other coding languages and about rounding.
For example, when I do the below, I don't get 0.000000000000000001 as an answer.
$a= 0.000000000000000002;
$b= 0.000000000000000001;
$balance = $a - $b;
What I get is -8.0E-20 which is not 0.000000000000000001 (i think).
Also, [from here][1], it says float has a max of 14 decimal places but then it used the word "roughly."
[1]: https://www.php.net/manual/en/language.types.float.php#:~:text=The%20size%20of%20a%20float,the%2064%20bit%20IEEE%20format).
To keep your precision when displaying, you must use a Float Data type, see the floatval() function for more help.
Up to 18 places is an arbitrary amount more than the supported size, which is 14 on most machines depending on memory?
As mentioned in the comments using defined base units would solve the problem, to expand on that to be a more generalized solution that is more scalable, applicable to more algorithms, is to create a new class to hold this data type, dynamic float (d_float) is what I would use, cc_value = new d_float(value: int, pof10: int); cc_value * pof10; Where crypto currency value is described by a integer value that contains each digit and a second integer that can be applied to return the correct decimal value. Different base amounts could simply be extensions of the d_float class and therefore have fixed pof10 values?
(This question is not about PHP type-casting.)
I have read in couple of questions what it is best not to show record id to users, but use another value, which doesn't give out any information about howmany record there is in the database, etc. I wanted to implement this, and after searching on google, surprisingly no solution was found.
So, my question is, is it possible to convert a (long, for example) sequence of strings to unique (or unique enough for at least million converts) sequence of numbers, is there any options available which I have no idea of?
Just to show you an example:
$uName = $this->newUsername;
$publicId = $this->strToInt(somecomplexstring); // Outputs something like 13272992
// Or feed with username
$publicId = $this->strToInt($uName);
You might considering using something like a slug. So your user will have an unique id in the database but also an unique slug (random string, ex. TGqJItemU5TGqJItemU5f6S5VaCr2n). You can then use this slug instead of the id when presenting data to the browser.
As stated in a comment, you can use uniquid to get a unique string. this function will return an hexadecimal string.
If you need only numbers, you just have to convert the hex to a decimal number with this hexdec.
The final code will look like this :
hexdec(uniqid());
An other way to get an integer from a string is to use md5. This function also returns a hex string so you will have to use hexdec to get a decimal number
Please note that a md5 is not a unique string, there is a (very small) probability of accidental collision (1 in 340 undecillion, see How many random elements before MD5 produces collisions ? for more info)
You can use:
md5(uniqid(rand(), true))
I have some phone numbers (validated such that all containing only integers, no - or + in them) as strings in a text file.
I am doing a simple mysql update on a mysql table which has phone number column as int(12). Note that I convert each phone number extracted from the text file to integer using intval().
The problem that I am facing is that instead of the numbers being inserted I just get 2147483647 to be inserted in each column. I guess I am making a small silly mistake somewhere, but still I can't figure it out. Can anyone explain what mistake am I making?
EDIT: Here is my the piece of code I am using (It does not give any sql error):
$sql="UPDATE ".$table." SET mobile = ".intval($smob).", phone = ".intval($sphone)." WHERE roll='".$sroll."'";
mysql_query($sql, $con) or die(mysql_error());
Excerpt from intval() manual:
The maximum value depends on the system. 32 bit systems have a maximum signed integer range of -2147483648 to 2147483647. So for example on such a system, intval('1000000000000') will return 2147483647. The maximum signed integer value for 64 bit systems is 9223372036854775807.
What you are getting is a maximum number for signed integer on a 32bit machine.
I would strongly advise you to not use this function for phone number conversions. One of the simple reasons is that my mobile number is 11 digit number (without leading + or double zero for international access), and I believe there are some countries that have even more digits.
Why you would need to store phone numbers as int's? Since most likely you are not doing some calculations and statistics on who might have a biggest phone number among your clients, and this data is probably used only for invoicing or a contact info, you could just leave that as a string.
Phone numbers are not integers, and should not be stored using an integer type.
The observed behaviour happens because the values you are trying to store are outside the range accepted by the column type. I recommend putting MySQL into strict mode - this forces it to return errors when inserted data, etc. cannot be stored, instead of silently trying to do whatever is the least bad thing without failing.
I have a form in which users can enter prices for items. Ideally I want the user to be able to add prices in whatever method feels best to them and also for readability. I then need to convert this to a standard float so that my web service can calculate costs etc.
The part I'm struggling with is how to take the initial sting/float/int of currency and convert it into a float.
For example:
UK: 1,234.00
FRA: 1 234,00
RANDOM: 1234
RANDOM2: 1234.00
All of those have slightly different formats.
Which I would want to store as:
1234.00
I will then store the result in MySQL database as a DECIMAL.
Any help would be great.
Assuming you're using MySQL, use the DECIMAL or NUMERIC type are the correct types used for storing currency.
Float's are susceptible to rounding errors and have a limited precision.
The formatting for display should be handled by PHP.
If storing in DB, you should of course store a currency code - which can be used when retrieving to tell PHP how to display it
Couldn't you use:
floatval($AnyVar)
In a case where you'd like to accept so many different formats it's a bit tricky to get it right.
Now we can just use a simple regex to get the decimal and full parts of the value:
/^([0-9,. ]+?)(?:[.,](\d{1,2})$|$)/
The regex will capture the full part of the number + a decimal part, separated with a , or a . and which has one or two numbers.
The capture group 1 will contain the full part, and group 2 the decimal part (if any).
To get your number, you just need to filter out all non-numeric characters from the full part, and join the filtered full and decimal parts together.
If you want to make it more foolproof, you probably should implement something on the client-side to guide the user to input the value in the correct format.
I need to compare a very large number in php (30 digits long) with 2 numbers in my database. Whats a good way to do this? I tried using floats but its not precise enough and I don't know of a good way to use large numbers in php.
Have you tried using string comparison? Just make sure every number is padded with zeroes.
mysql> select "123123123123123123456456456"<"123123123123123123456456457";
+-------------------------------------------------------------+
| "123123123123123123456456456"<"123123123123123123456456457" |
+-------------------------------------------------------------+
| 1 |
+-------------------------------------------------------------+
Justed test this up to 200+ chars, works like a charm.
Check bcdcomp function
You could compare strings instead.
Depending on how you're fetching the data from the database, you may want to explicitly cast the integer to a string type in the SQL statement.
Other than that, there are several libraries in PHP that handle large integers, like BCMath and GMP.
Handling large numbers in PHP is done through either of two libraries: GMP or BC Math.
I haven't done this myself, so it may not be correct, but I think you'd have to take the string result from GMP or BC Math, and feed that into the query. Make sure you store your numbers as bigint.
Interestin fact: You might think BigInt would be limited to about 20 digits, and you'd be right, except for the fact that it has Mysql Magic:
You can always store an exact integer value in a BIGINT column by storing it using a string. In this case, MySQL performs a string-to-number conversion that involves no intermediate double-precision representation.
If they're -very- big, I'd compare them as strings even. First, if one is longer than the other, it wins. If they're the same length, compare digit by digit left-to-right - if two digits differ, the number with the bigger digit wins. This of course for Positive integers.