This question already has answers here:
int((0.1+0.7)*10) = 7 in several languages. How to prevent this?
(7 answers)
Closed 9 years ago.
Can anybody figure out how the following statement evaluates to 7?
echo (int)( (0.1+0.7)*10 );
I was trying the operator precedence in PHP. So, if there is anybody who can help, it will be highly appreciated.
If you remove the (int) part, and run the follwing code instead:
echo number_format(((0.1+0.7)*10), 20);
The output will be 7.99999999999999911182. This value parsed to an integer will result to 7, as parsing a value to an integer will always floor the value.
Reading the following article should give you an idea of what's going on here.
In short, double values are always a binary value, and through that a 'product of 2^n', whichever will be the nearest to the decimal you said it should be. And with 2^n you dont have any chance to reach exactly 0.1.
Related
This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 5 years ago.
I'm currently working on retrieving data from coinmarketcap API and I would like to limit the strings to 5. I retrieve the data and then multiply the value *1.07 and the result to MXN, but the string is long, like 25.65675734343, I want to limit that string, this is my echo:
echo $xrpprice*$rate*$fxrates['rates']['MXN'];
Use round:
$result = $xrpprice*$rate*$fxrates['rates']['MXN'];
echo round($result,2);
In PHP you can use number_format to control how many decimal places. Assuming you don't want to limit the whole number to just five characters.
echo number_format($numberToFormat, $numberOfDecimalPlaces);
Please better explain your question. Also, what have you tried?
This question already has answers here:
How to fastest count the number of set bits in php?
(6 answers)
Closed 8 years ago.
If i have 2 binary number representation: 127 and 128. How can i calculate that 127 have 7 bits "ON" and 128 have only 1 bit "ON"?
I did it like the following, but i think there's probably a better way (with math):
strlen(str_replace('0','',decbin(127))); // 7
strlen(str_replace('0','',decbin(128))); // 1
It looks like what you want is a population count. The Wikipedia reference has some code, and for problems like this I always check Bit Twiddling Hacks which is a great reference.
There are asm instructions for this on some machines, and both gcc and MSVC have compiler builtins.
For other languages, see: Population Count on RosettaCode
This question already has answers here:
PHP float/double stored as MySQL DECIMAL
(2 answers)
Closed 8 years ago.
I have a table with a DECIMAL(10,6) column. I have a string representation of a float (e.g. 35.3123122). I am using php to insert the value. I used floatval($string) to insert and it did not work. I also tried to insert the string itself with no success. Do I need to format the float value exactly to match the 10,6 criteria. If so, How can I do this?
Thanks
Not sure if this is your problem, but the value of "35.3123122" has 7 digits after the decimal point. DECIMAL(10,6) is 10 digits in total with a maximum of 6 digits past the decimal point.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Forcing output in standard mathematical notation in PHP
Output displayed: 2e+008
How to avoid displaying the epsilon value in php?
Output needed: 200000000
mysql table
amount float
if i enter 200000000 in input box it stores 2e+008 in mysql and just fetch the value through jquery using this code
$amount=$("[id=amountfee]").val();
<?php echo $amount ?>
You should look at the number_format() function: http://us2.php.net/manual/en/function.number-format.php
use DECIMAL or DOUBLE as data type instead of float. This will also help you to avoid problems you might have on float.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Converting a number (1, 2, 3) to a string (one, two, three) in PHP
I have a form in which products will be listed out when,i clich total chk box it will be calculated but,it will be integera only,i want this to print in words.
For eg:217 it should print in words two hundred seven only. Hope,this is clear.
Thnx in advance
PEAR's Numbers Words is old, but competent.
The duplicate question seems to answer this quite well, DAFFODIL, have a look there. You will have to have PEAR installed. Easiest way is to try Numbers_Words::toWords(200) and see if that throws up an error.