I'm using minus php query to calculate and find out two variables difference. Here is the code i'm using to calculate
Example
$v1="13.240";
$v2="0";
echo $v1 - $v2;
When calculation gets completed, the zero at the end of variable gets stripped eg (13.24), which in right scenario should have been 13.240. I do not understand how to fix this issue.
Both are same, but if you need that badly, then you can use number_format() to keep number 3 decimal places,
<?php
$v1="13.240";
$v2="0";
echo number_format($v1 - $v2, 3);
?>
DEMO: https://3v4l.org/FV1Or
Related
I have a variable $percentchance that represent the percent chance to succeed with something. What I would like help with is, for it to never display above 100 or below 0.
Currently I have used this min function to never go above 100.
<?php echo min(100, $percentchance); ?>
Another issue is that sometimes I get the value of percent like 26.3456, and I wish for some way to make it round up or round down and display only 2 decimals like 26.35 in that case.
You have to set an upper and lower bound for the first problem. The second problem can be tackled with number_format.
Example:
echo number_format(min(100, max(0, $x)), 2);
To enforce always rounding up to the next second digit (e.g. 1.111 would be rounded to 1.12), you could utilise ceil.
echo number_format(min(100, max(0, ceil($x*100)/100)), 2);
if you need to round up with just two decimal use round()
echo round(26.3456, 2);
I am adding 2 prices together (which are session variables) in php and I want it to show 2 decimal places. The session variables themselves show as 2 decimal places but when added together and for example the result is 2.50 only 2.5 is displayed. Is their a way I can display the two decimal places? This is the code I am using
<div id="info">
<span class="bluetext2">Total: </span>$<?php echo $_SESSION['startingPrice'] + $_SESSION['postage']; ?><br>
</div>
You have a couple of options here.
number_format - will output the decimal places, can also be used to specify a decimal point character as well as a thousands separator.
echo number_format($x, 2);
printf/sprintf - These are identical except that printf output whereas sprintf returns
printf('%.2f', $x);
echo sprintf('%.2f', $x);
money_format - Locale aware money formater, will use the proper decimal and thousands separators based on locale.
setlocale(LC_MONETARY, "en_US");
echo money_format("%i", $x);
You can try this :
$Total = number_format((float)$number, 2, '.', '');
use echo number_format("2.5",2);
Use number_format function. This code:
echo number_format(12345.1, 2, ".","");
will give result: 12345.10
Also you can use short version:
number_format(12345.1, 2)
which results in 12,345.10 (I think that is english format ... )
As simple as -
if u store that as an integer
like $total=5.5000 at the time of displaying it will display 5.5.
If u use is as $total="5.5000" then it will display as 5.5000
OR
$asdf=$x+$y; //5=2.50+2.50
echo number_format($asdf,2);
Possible duplicate to
PHP: show a number to 2 decimal places
Use number_format((float('number to be rounded off'),' number of decimal places to be rounded off','separator between the whole number and the number after the separator')
example
$foo = 150
echo number_format(float($foo),2,'.')
will give 150.00
I'm building a site where people can exchange coins (site currency) into Bitcoin. The problem I'm having is that for some reason when I multiply the $btcprice with 3 or less the echo is really weird... for your sake this is the code that matters:
<?php
// get 0,01 usd in bitcoins into a variable
$btcprice = file_get_contents('https://blockchain.info/tobtc?currency=USD&value=0.01');
$valueInBTC = 4 * $btcprice;
echo $valueInBTC;
?>
Anything that's 4 or higher will work, but if you try to multiply this with 3 or less it gets weird. For example this:
<?php
// get 0,01 usd in bitcoins into a variable
$btcprice = file_get_contents('https://blockchain.info/tobtc?currency=USD&value=0.01');
$valueInBTC = 3 * $btcprice;
echo $valueInBTC;
?>
Will echo 7.959E-5
I just don't understand what the problem is...
The result you are getting is not an error. It is simply in a formatting you don't expect / know yet. 7.959E-5is exactly the same as 0.00007959 it is just a different way of writing it down. Think of it as 7.959E-5 = 7.959 × (10 ^ (-5)) = 0.00007959. It is called Scientific notation (E notation). In cumputation / science this notation is used, because you can show very large or very small (as in your case) numbers with less digits (it is just shorter to write).
To get the number in other formattings use the php function sprintf().
As you are handling bitcoin values, you shouldn't be formatiing the numbers until just for output. With bitcoins you always deal with very small numbers and you will soon meet precision problems if you try and calculate with formatted floating point numbers.
In most cases this is a formatting issue. You can simply use printf
printf("%.2f",$valueInBTC);
Another good option is to use number_format();
Eg:
$number = 1234.5678;
// english notation without thousands separator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57
In php I want some calculation part to be done. So I am getting all the values from variable and doing calculation. When doing calculation my formula is something like this
ceil($99.00/100)*2
but here it is showing error as $(dollar currency symbol is there). So can someone kindly tell me what is the good method of doing calculation here?
You need to learn basic PHP. You can't feed a monetary string (99 dollars and zero cents) into math operation. PHP will attempt to use $99 as a variable, and variables cannot be named with numbers.
You're basically doing
ceil (99 dollars concatenated with (zero divided by one hundred)) times two
If you're trying to do actual math with numbers, then
ceil(99/100) * 2
is all you'd need.
So basically you are dividing 99 dollars 0 cents with one hundred and multiplying the result by 2.
Try this:
<?php
$amount = 99.00;
$calculation = ($amount/100)*2;
echo $calculation;
You should append the dollar sign after you have done the calculation. Like this:
echo '$'.$calculation;
Remove the $. PHP can't calculate with anything but pure numbers, so $99.00 obviously won't work for 99 dollars.
Actually, what PHP thinks you are trying to do is have a variable (variable names start with a $). But 99 is not a valid variable name. Then PHP thinks you want to concatenate that variable with the result of 0 / 100 (concatenation is done with ..
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
?>