MySQL mod vs PHP mod [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Edited
Sorry guys I want PHP mod to display decimals
In MySQL select mod(15.6,1) result 0.6 but in PHP 15.6 % 1 result 0?
My purpose is to restrict calculation only for exact integer which does not allow decimals

In PHP, the % modulus operator works on integers. The operands 15.6 and 1 are cast to int (15 and 1) before being worked on. Therefore, 15 % 1, as you may expect, gives 0. Therefore "exact integer which does not allow decimals" matches the behaviour in PHP.
In MySQL, as the documentation says:
MOD() also works on values that have a fractional part and returns the exact remainder after division
Therefore, you should TRUNCATE(operand, 0) the operands before using them in MySQL to obtain the integer behaviour. This is consistent with PHP's behaviour of casting to int -- numbers are truncated, not floored (which is a significant difference with negative numbers).

Related

How should I correct ill-formatted lat-long pairs to be valid for PHP computation [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have access to a dataset that contains latitude and longitude pairs, but they are ill formatted and are not properly represented in the data set.
For example, a lat-long pair might look like this: 31333445, 105530865 when it should be 31.333445, -105.530865. Given the data set I am working with I know that the min value for latitude is 31.0 and the max is 37.0, and the min/max of longitude is -103 to -109.
If I was given a piece of paper and a pencil I could easily correct these myself, but the correction needs to happen on the fly when we receive input from a different program. We have no control over how the data is formatted until it hits our system and then we can make changes and corrections, and the lat-long pairs are all in a integer format listed above rather than a float.
What would be the best way to go about manually correcting this error? I am using PHP for our processing system.
If they're the same length then just divide by 1000000 and make negative where needed:
echo $lat / 1000000;
echo -$lon / 1000000;
If not then get the number of numbers at the start (2 and 3 here) making negative if needed, then insert a decimal and the remaining:
echo substr($lat, 0, 2) . '.' . substr($lat, 2);
echo -substr($lon, 0, 3) . '.' . substr($lon, 3);
You can use floatval() on the results if needed.
If the number of digits is always the same fixed size then use the solution suggested by #AbraCadaver in his comment ( just divide by 1000000 and multiply with -1)
If the number of digits can be different you need a different solution
and I have got a weird idea (at 0:24am)
I would convert the number to a string resulting in "31333445"
then concatenate "0." with the "31333445" resulting in "0.31333445"
then convert it back to a double resulting in 0.31333445
and then multiply it with 100 resulting in 31.333445 (and multiply the other value with -1 )
:-B
With this solution it does not matter if the number you get from outside has 3 or 14 digits
May sound weird but should work.
If this sounds to be a useful solution i will put into code tomorrow.

Convert double timeformat with php MSSQL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have here a problem to convert a time who is double with php. I´ve never seen it before.
Is this the timeformat from excel?
Is it possible to do it with the mssql server where the data is stored in?
Here are some time examples.
0.291666666666667 = 07:00:00
0.229166666666667 = 05:30:00
0.320833333333333 = 07:42:00
Guessing those are times? Use CONVERT:
WITH VTE AS(
SELECT *
FROM (VALUES(0.291666666666667),(0.229166666666667),(0.320833333333333))V(T))
SELECT T, CONVERT(time(0), CONVERT(datetime, t)) AS TimeValue
FROM VTE;
(2 conversions, as you can't convert straight from a decimal to a time)
Edit: Datetime values in Excel (and sql server) are actually stored as numerics. The value 0 for a datetime in SQL Server being 19000101 (if I recall correctly, 0 is 19000100 in Excel/Access and 1 is 19000101; just to make things a little confusing). Each full integer adds an extra day to the datetime 0. Thus a value of 1 is 19000102 and 5 would be 19000106, etc, etc.
The same is true for the time portion, however, these are stored as the decimal part. Thus, 0.5 represents 12 hours. Thus, the value 0.5 represents 19000101 12:00:00.000. 6.75 would be 19000107 18:00:00.000. With Excel and Access, they also stores times as a decimal (between 0 and 1). Hence, why the above works, as it firstly converts the decimal to a datetime (for the first value 19000101 07:00:00.000) and then to a time(0) (0 represents 0 points of millisecond accuracy); resulting in 07:00:00.

Number formating - always 2 to 4 decimals after decimal separator and with thousand separator [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have number:
2000
100
0.1
0.15
0.1555
But I need show these number in this format:
2,000.00
100.00
0.10
0.15
0.1555
I will never have more than 4 decimals after decimal separator.
If after decimal separator are only null, I need to display 2 nulls (3. example above).
How I can format these numbers to get required format?
Thank you!
EDIT: I can't use just number_format function, because with this function I can only set how many decimals will be after decimal separator, but as I wrote above - sometimes there will be 2, sometimes 4. So, if my number is 2000.0001 and I set this number_format(2000.0001, 4) output will be 2000.0001, but other number like 0.1 will output 0.1000, but I need it with 0.10 format.
First use number_format to add the commas and get 4 digits after the decimal. Then use a regular expression to remove up to 2 trailing zeroes.
$formatted = preg_replace('/0{1,2}$/', '', number_format($input, 4));
DEMO

questions related to source code of PHP uniqid() [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
In PHP source code function uniqid() have following C code:
(I removed some types to shorten it)
//...
struct timeval tv;
gettimeofday(&tv, NULL);
int sec = (int) tv.tv_sec;
int usec = (int) (tv.tv_usec % 0x100000);
// The max value usec can have is 0xF423F,
// so we use only five hex digits for usecs.
printf("%08x%05x", sec, usec);
//...
If we put criticism aside, they try to produce 64 bit timestamp.
0xF423F is probably CLOCKS_PER_SEC - 1 (CLOCKS_PER_SEC is decimal 1000000),
but where this 0x100000 come from and what could be the reason to use modulus instead of bitwise and?
She or he could write the Unique ID as printf("%08x%08x", sec, usec)
sample output:
55189926000eb16f
5518997900051219
5518997a0005171b
The zeros in position 8 to 10 are consistent, they don't add entropy, so he wants to get rid of those zeros. The new UID will be 3 bytes shorter with the same entropy. He could simply use printf("%08x%05x", sec, usec);
sample output:
55189926eb16f
5518997951219
5518997a5171b
But that's on the assumption that usec is guaranteed to be less than 0x100000 otherwise UID will be up to 16 bytes long. You need % 0x100000 for insurance. It's also the same as & 0xFFFFF. Technically the insurance should be % 1000000 (decimal), but it doesn't really matter, it's still the same entropy.
Or we could just use the 16 byte version because saving 3 lousy bytes don't matter these days.

All integers in the series will lie in the range [-10^6,+10^6] means? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I found the one of the condition as "All integers in the series will lie in the range
[-10^6,+10^6]" in one of the online test what does it means?
From the Wikipedia page on intervals you can see;
A closed interval includes its endpoints, and is denoted with square brackets. For example [0,1] means greater than or equal to 0 and less than or equal to 1.
10^6 (10 to the power of 6) is the same as 1 million, so All integers in the series will lie in the range
[-10^6,+10^6] means that all numbers in the series are;
Greater than or equal to -1000000 (minus one million)
Less than or equal to 1000000 (one million)

Categories