why does the next operation fails to show me a float with 8 decimals?
echo round(round(10/100,8) * round(0.00003,8),8);
echo '<br>';
echo round(0.1 * 0.0003,8);
outputs
3.0E-6
3.0E-5
Basically I'm trying to get 10% value out of a float number.
Thank you!
The result is correct: it's the exponential notation. Essentially the number following the E is the exponent on base 10. If you need to print the decimal digits there is a specific function for that, which is number_format.
number_format(round(round(10/100,8) * round(0.00003,8),8), 8)
Related
AS the title says: using this number 1000000.125685, how can I format it like 1,000,000.125 without using number_function, since that function rounds the decimals?
number_format("1000000.125685",3) //returns 1,000,000.126
How can I give whatever number I want 'Money Format' without using number_format?
If you use floor() on the number prior to formatting it, this will get what you want. The problem is that floor() rounds fractions and not 'to n decimal places'. So this code does the round trip of *1000 and then /1000...
echo number_format(floor(1000000.125685*1000)/1000,3);
gives...
1,000,000.125
To wrap this up into a function with the number and decimal places as parameters...
function number_format2( float $n, int $dp ): string {
$multi = pow(10, $dp);
return number_format(floor($n*$multi)/$multi,$dp);
}
echo number_format2(1000000.125685,4);
Need to round 30.61 to 30.60, Any built-in function for PHP to do this ?
If I understand your desired output correctly, that you only want to round the second decimal point, you can round with 1 decimal presicion, then use numer_format() to ensure you get the correct number of decimals.
$num = 30.61;
echo number_format(round($num, 1), 2);
round() documentation
number_format() documentation
Live demo
you can do this
$num = 3.61;
/*round to nearest decimal place*/
$test_number = round($num,1);
/* ans :3.6
format to 2 decimal place*/
$test_number = sprintf ("%.2f", $test_number);
/* ans : 3.60 */
I need execute json_encode() and convert my original number from:
50610101800060384093800100001010000000056199999999
to
"50610101800060384093800100001010000000056199999999"
But it return
5.061010180006E+49
I tried this:
ini_set('precision', 30); //With 1, 30, 50, 100, 1000
ini_set('serialize_precision', -1);
'content' => json_encode($params, JSON_NUMERIC_CHECK)
but doesn't work. Can you help me?
50610101800060384093800100001010000000056199999999 exceeds the value of the maximum integer in PHP and so it is promoted to a float and expressed in scientific notation. The float result may be problematic for various reasons as the Manual explains in warning about floating point precision.
If you wish to express the value as if it were an integer you must encapsulate it in a string. That string you may add zero to it but when you do so the result in scientific notation will refer to a float, as follows:
<?php
$s = "50610101800060384093800100001010000000056199999999";
echo $s,"\n";
$x = $s + 0;
echo $x, "\n",is_float($x);
See here.
For more info in re PHP and floats, see here.
On the other hand, if there were an array of numbers whose digits corresponded to the numerical display in the OP's post, you could write code as follows:
<?php
$a = [5,0,6,1,0,1,0,1,8,0,0,0,6,0,3,8,4,0,9,3,8,0,0,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,5,6,1,9,9,9,9,9,9,9,9];
foreach($a as $e) {
$e = (string) $e;
}
$foo = join($a);
var_dump($foo);
$foo = bcadd($foo, 1);
var_dump($foo);
See live code.
The reason this example works is because each array value is converted to a numerical string and then the individual elements are joined to form one very long numerical string. BC Math is an extension in PHP which supports arbitrary precision. In this case, the bcadd() function adds one to the numerical string which results in the display of an incremented numerical string value.
Try This [https://3v4l.org/biNJG][1]
If you want this output "50610101800060384093800100001010000000056199999999"
you may want to pass this Value as string after encoding the value to JSON using json_encode
An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647.
Rules for integers:
An integer must have at least one digit
An integer must not have a decimal point
An integer can be either positive or negative
Integers can be specified in three formats: decimal (10-based), hexadecimal (16-based - prefixed with 0x) or octal (8-based - prefixed with 0)
$quantity = 20;
$product_rate = 66.79;
$total = $quantity * $product_rate;
echo $total;
Output is showing 1335.8000000000002
is there possible to show 1335.8 using php..?
You can use the number_format() function like this:
$firstNum = 1335.8000000000002;
$number = number_format($firstNum, 1, '.', '');
echo $number;
outputs:
1335.8
more on number_format() here: http://php.net/number-format.
You can also multiply the number by 10, then use intval() to convert it to an integer (that way stripping out the decimals) and then divide by 10 like this:
$firstNum = 1335.8000000000002;
$number = 10 * intval($firstNum)/10;
echo $number;
outputs:
1335.8
Note: when using the methods above there will be no rounding, for rounding you would use something like this:
$number = round($firstNum, 1);
echo $number;
which in this case also outputs:
1335.8
Do you really use these variable values? I'm using PHP7 and the output for your given values is 1335.8. If you do a manual calculation it is the same result. It should be 1335.8. Anyway if you need to roundup the value you can use below.
round($total,1);
Please refer the below link and you will be able to grab more details.
http://php.net/manual/en/function.round.php
Because how floating point numbers work, they cannot represent every numbers exactly, so approximations are made.
The closest representation of 20 is 20, it can represent 20 exactly, but 66.79 for instance is approximated to 66.7900000000000062527760746889, that times 20 is 1335.800000000000125055521493778 that again cannot be represented and is approximated to 1335.80000000000018189894035459.
Depending on how you choose to print this number, it may round different ways, in your case for some reason you decided to print 13 decimal places so it rounded to 1335.8000000000002, but if you print only 1 or 2 decimal places it will print as 1335.8 or 1335.80. Just be mindful about that when printing floating point numbers, you may want to specify how many decimal places are relevant to you. For that, use number_format().
Example:
echo number_format($number, 2); // prints 2 decimal places
You can do this simply using echo echo round($total, 1) instead of doing round($total)
print_r(bin2hex("11111111"));
echo '</br>';
print_r(bindec("11111111"));
Result
131313131313131
255
I want a hexadecimal 16 byte value to do aes encryption.How is the conversion from binary to hex happening in php.I am getting incorrect value using the function.Also when i convert an array of hexadecimal values to string the byte length changes
You get a correct result, it's just not what you want. bin2hex() returns an ASCII string of the hexadecimal representation. A quote from the manual:
Returns an ASCII string containing the hexadecimal representation of str.
So If you want the hexadecimal number you can use this:
print_r(dechex(bindec("11111111")));
The converter to get hexidecimal is dechex(), but it needs a decimal number. To do that we convert you binary string to a decimal number first using bindec() and then pass it into dechex(), e.g:
print_r(dechex(bindec("11111111")));
<?php
$str = "Hello world!";
echo bin2hex($str) . "<br>";
echo pack("H*",bin2hex($str)) . "<br>";
?>
PHP.NET Manual :
http://php.net/manual/en/function.bin2hex.php
Test Your Result : http://www.cs.princeton.edu/courses/archive/fall07/cos109/bc.html
Detailed Explanation:
http://www.computerhope.com/binhex.htm
It's simply 9 * 16 + F where F is 15 (the letters A thru F stand for 10 thru 15). In other words, 0x9F is 159.
It's no different really to the number 314,159 being:
3 * 100,000 (10^5, "to the power of", not "xor")
+ 1 * 10,000 (10^4)
+ 4 * 1,000 (10^3)
+ 1 * 100 (10^2)
+ 5 * 10 (10^1)
+ 9 * 1 (10^0)
for decimal (base 10).
The signedness of such a number is sort of "one level up" from there. The unsigned value of 159 (in 8 bits) is indeed a negative number but only if you interpret it as one.