I have the following variable in PHP:
$number = 1714.2
As you can see with the last decimal I would like to add a 0 if the last decimal number is lower than 10.
What I would like my variable to look like:
1714.02
as .2 and .02 is not same. so, you must have to take correct value form source
or maybe php-manual number format is helpful to you. http://php.net/number_format
1714.2 is not equal to 1714.02
1714.2 means 1714.20 which means 1714.200
But if you want to get the desire result you want just put this code:
$number = $number - 0.18;
If you have a typo and you tried to say:
1714.2 being shown as 1714.20 then refer to:
Set precision for a float number in PHP and number_format
Related
I have a number 00101 when I print out this number (or using it for my purpose) I got 65, I've tried intval() but it also returns 65. Can anyone explain to me why? And what is the easiest way to get 00101, or 101?
I would say you are using an invalid type of number, there is bits type (see the list of types in https://www.php.net/manual/en/langref.php)
if you run the following
$a = array(10101, 11100, 11010, 00101);
var_dump($a);
you will see that PHP convert your number to int 65
so maybe you want to use strings?
You will get 101 from string '00101' when passing to intval function. However an integer number does not have start with leading 0; PHP does not get it as decimal number.
I have read the documentation and have clear idea of round but i didn't find useful information to solve the problem.
The problem is i have float number which is let say 1.09 and i want to display it 2 instead of 1. if we use round function it display 1. Help me solve this problem.
MORE DETAILS...
$TotalPaidRemaining=1090;
$monthly_installments=1000;
$MakingNumberOfMonths=$TotalPaidRemaining/$monthly_installments;
echo round($MakingNumberOfMonths, 0, PHP_ROUND_HALF_UP);// it display 1. i want it display 2..
What i want is if the value after decimal point is greater than 0. For example 0.01. I want to consider it as 1.
Hope i am clear at my question.
Use the ceil() function instead.
$number = ceil(0.1); // $number will be 1
From the documentation :
Returns the next highest integer value by rounding up value if necessary.
You can use the ceil() php function instead of round(). It will round up your values. Docs: http://php.net/manual/en/function.ceil.php
Example:
ceil(1.09); // return 2
You could use ceil($yourNumber), which will round the number to its next higher integer.
Or you could use round($yourNumber + 0.499999999999999).
Or you could use floor($yourNumber + 1), which rounds the number to its previous highest integer.
I am reading a value from excel file when it is a number >= 14 digit it convert this to something like 3.5775004173581E+14 I want to get the exact value in decimal like 3.57750041735819 I have tried
(float) 3.5775004173581e+14 O/P 3.5775004173581e+14
intval((float) 3.5775004173581e+14) O/P 740815490
number_format(3.5775004173581e+14) O/P 3.57750041735810
but former return the same string as O/P second one produces some garbage value where as works well for the exponent <14 and the number_format adds traling 0 after 14 digits.
That's because float precision setting is default 14. To change this, use ini_set(), for example.Then you'll be able to get proper values. Sample:
$strVal = "1234567890.123456789";
//float(1234567890.1235), because
//default precision is 14:
var_dump((double)$strVal);
//float(1234567890.123456717)
ini_set('precision', 19);
var_dump((double)$strVal);
This is not only about decimal precision, but about float precision
:
$strVal = "1234567890123456789";
var_dump((double)$strVal);//float(1.2345678901235E+18)
ini_set('precision', 19);
var_dump((double)$strVal);//float(1234567890123456768)
Also, important note - it seems that trying to overcome precision in your case is an attempt to resolve symptoms, not the problem. So you should choose correct data model rather than try to solve this "problem".
I have a simple question about variable type in php. I have two values in my array:
$row['DS'] // type :float (with one decimal like 12.2)
$row['TC'] // type :float (with one decimal like 24.2)
What I'm actually try to do in the make the calculation below:
$row['TC'] / $row['DS'] // $row['DS'] need to be as integer (without point,like 12)
and the result should be with two decimal like (2.32). I tried to do it in that way
$DSF = number_format($row['DS'],0);
$ConF = $row['TC'] / $DSF ;
echo number_format($conF,2);
but it returns the wrong result. for example :
$row['DS'] = 59,009.3 ---> after change the format is change to 59,009
$row['TC'] = 190.0
$ConF = 190.0 / 59,009
it should be 000.223 (something around this number ) and i expect to get 0 (after i change the format using number_format($conF,2) but instead of this the program return me the number 3.22
What am I doing wrong?
The function number_format() is used to format numbers to a comma style representation, not to actually round numbers to what you want.
The function you are looking for is round which returns a float to a specified number of decimal places.
For example:
$yourVar=round($row['TC']/$row['DS'],2);
This means that $yourVar will be the value of the division rounded to two decimal places.
You should use the number_format() function only to display human-friendly numbers at the end.
You can use type casting to convert $row['DS'] to integer right in your calculations, for e.g.:
$row['TC'] / (int)$row['DS']
or
$row['TC'] / intval($row['DS'])
I have the following line of code in javascript:
(Math.random() + "") * 1000000000000000000
which generates numbers like:
350303159372528000
I tried the same thing in PHP with this:
rand()*1000000000000000000
Which returns:
2.272e+21
I need to use PHP as the number generated will be stored as a SESSION variable and will be used by JavaScript later on.
How do I get PHP to force the number to be an int rather than a float?
EDIT PHP seems to struggle with this.
Would it work if I just generated the rand number in PHP saved it to the SESSION and then done the multiplying by 1000000000000000000 in JavaScript?
How would I go about this?
I'd recommend calling
PHP_INT_MAX
To see if your PHP installation can handle an integar that large. I'm guessing it can't which is why it is knocking it down to scientific notation.
I'd suggest converting your result to an int:
intval(rand()*1000000000000000000)
That said, see Kolink and Jeremy1026 answers for precision issues. If you only need an unique identifier, see Truth's answer.
Update: if you're using strings to represent your numbers, don't want or can't use an arbitrary precision library, and don't stricly need perfecly fair random numbers, you could generate smaller numbers and concat them together:
strval(rand()*999999999 + 1) . strval(rand()*1000000000)
(The +1 is to avoid a leading zero in your result; note also that your number will never have a single digit, but every other number is possible)
For a random number with (exactly) 18 digits, you can also use str_pad in the 2nd part, to fill it with leading zeros:
strval(rand(100000000,999999999)) .
str_pad(strval(rand(0,999999999)), 9, "0", STR_PAD_LEFT)
If you need a unique identifier (which is what it looks like you're trying to do), please use PHP's uniqid() function.
floor() / ceil() / round() / (int) / intval() will convert the number to int.
Also, rand() takes two arguments. If ints are supplied - it will return an integer
And printf() should take care of printing in the format you wish (printf('%d', $int) should do the trick)
In the end I solved the issue like this:
<?php
error_reporting(0);
function RandNumber($e){
for($i=0;$i<$e;$i++){
$rand = $rand . rand(0, 9);
}
return $rand;
}
echo RandNumber(18);
// Outputs a 18 digit random number
?>