I'have a $value like that var_dump(bin2hex($value)) output = '015180'
I want the convert this value to decimal. So I know my $value is binary, I write those codes:
var_dump(bindec($value));// 0
var_dump(hexdec(bin2hex($value)));//86400
Result is 86400 but why don't the first one returns me 0. what is the different between two of them. Or Am I missing something ?
I'm only quoting one of comments in bin2hex documentation:
bin2hex function is for converting binary data into a hexadecimal string representation. This function is not for converting strings representing binary digits into hexadecimal.If you want that functionality, you can simply do this:
<?php
$binary = "11111001";
$hex = dechex(bindec($binary));
echo $hex;
?>
This would output "f9". Just remember that there is a very big difference between binary data and a string representation of binary.
Author: tehjosh
Related
I have written a CSV importer and some of the values are very small, for example, in one of the CSVs I have 0.000024 which is being formatted as "2.40E-05" and when PHP puts that into the database it stores it as 2.4, I found an article that said if you add 1 to it, it changes to a float which is what I did and I got 1.000024, but if I then subtract 1, it goes back to being 2.4E-05, what is the best way in PHP to convert a scientific notation string to an actual float value? I would like to be able to store the value in my database as 0.000024.
You should convert to a string otherwise if you keep the float it will keep displaying with scientific notation
$scientific = "1.828331E-9" ;
$num=explode('-', $scientific);
$precision=$num[1]+ strlen(filter_var($num[0], FILTER_SANITIZE_NUMBER_INT))-1;
$float = number_format($scientific, $precision);
echo "converted $float and back to original " . (float)$float;
I just used number_format($revenue, 6) and it stored as 0.000024.
I know the question is very basic but it seems nothing working for me.
I have a number (either or float or integer) which I want to be formatted upto two decimal point. For this purpose I'm using PHP function number_format but it converts my number to string.
To convert it back to float I am using (float) or floatval(). But these functions just truncates the number after converting it to float from string.
Here is my code
$revenue_sum = array_sum(array_column($val2, 'weighted_revenue')); //23722
$test = number_format($revenue_sum, 2); //"23,722.00"
$test = (float)number_format($revenue_sum, 2); //23.0
$test = floatval(number_format($revenue_sum, 2)); //23.0
I want the $test to be 23722.00 for the $revenue_sum = 23722
If $revenue_sum = 2372.2 the $test should be 2372.20
number_format() function can be used as follows:
echo number_format($revenue_sum, 2,'.',''); // will return 23722.00 for the $revenue_sum = 23722
You are trying to type cast with ',' value, it is truncating the string.
you can try this
<?php echo sprintf("%2.2f", 8900.258); ?>
which will output as
8900.26
If you assign a floating point value to a variable, then it is converted to an internal binary format (usually using IEEE 754). Not all possible values has an internal representation. So while scanning a text, the float is rounded to the nearest possible value. So for example 1.23 is rounded to 1.22999999999999998.
Because of the internal representation, there is no difference between 100 or 1e2 or 100.0 or 100.0000.
And when printing a floating point value without any formatting instruction, PHP guess a good format and rounding some digits. So 1.22999999999999 is displayed as 1.23(may varies on different systems).
In general: As long you are calculating, formatting doesn't matter. It is mostly the best, to ignore the decimal fragments on debugging. But when printing (=converting to text), use functions like format_number() or any of the printf() functions.
To be more pragmatic:
I tried to convert a 64-bit binary to decimal.
For example with input like this :
"0011000000110000100000011001110011100011000111101110001101001011"
I should obtain this output :
"3472417823527985995"
But the results obtained are inconsistent with expectations. The result obtained from the example input is
"3472417823527986176",
Here is my code:
<?php
$b = '0011000000110000100000011001110011100011000111101110001101001011';
echo sprintf("%.0f", bindec($b));
$td = 4.0;
echo $td;
The output is 4;
But I want real number (4.0) in double data type;
First, not to be nitpicky, but PHP doesn't have the type you want*. When you do $td = 4.0; you have created a float.
If you inspect it with var_dump($td);, you'll see: float 4. Since there isn't really a concept of significant figures here, the zero after the decimal is not relevant to the stored value.
Second, when you do echo $td;, PHP will output the string representation of float 4. Again, without somehow specifying that you want to display n decimal places, PHP will omit any trailing zeroes. For another example, if you did this
$td = 4.00010000;
echo $td;
You'd see
4.0001
This is why the other answers/comments are guiding you toward a formatting solution. Because what you're really needing to do is not to change the type of the variable, because it's already stored in an appropriate type. You just need to specify how it should be displayed when it's converted to a string. There are different ways to do that. If you use printf, you can specify a number of decimal places to display. Here's how you make it show one, for example:
printf('%.1f', $td);
The '%.1f' is a format string. The .1 part is what tells it to show one decimal place. But you aren't changing the type. It's just output formatting.
*Here's a list of PHP's native types. And I was sort of mistaken, it does indicate that float is aka double.
You can use printf
echo printf("%f\n", $td);
Check this out -> string number_format ( float $number [, int $decimals = 0 ] )
Doc: http://php.net/manual/en/function.number-format.php
$td = 4.0;
echo number_format($td,1);
this will spit out 4.0 the "1" is the number of decimals you want in the number
I have array like:
$array = ['id' => '76561198165327575'];
And I need it to work in JavaScript on client side. So I'm trying to encode it with JSON_NUMERIC_CHECK:
json_encode($array, JSON_NUMERIC_CHECK);
And getting result like:
{"id":7.6561198165328e+16}
But it should be:
{"id":76561198165327575}
What is wrong?
(Azure, Windows, 5.6)
JSON_NUMERIC_CHECK basically tells the encoder "If it looks like a number, encode it as a number":
php > $x = '123456789012234567890';
php > echo json_encode($x, JSON_NUMERIC_CHECK);
1.2345678901223e+20
php > echo json_encode($x);
"123456789012234567890"
And since your number exceeds the representable range for an INT on your platform, you get a float instead.
You are exceeding the bounds of integer on your 32-bit system. The documentation describes that when this occurs, the number is converted to a float.
If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead.
If you encode in JSON objects with large numbers (greater than PHP_MAX_INT), you will always end up getting a floating point value. The only solution is to store them in the object/array as string (that you already) and not use JSON_NUMERIC_CHECK (but convert the string to a number on the client) or write your own encoding routine.