I set int data type in mysql database but it not showing decimal place like 10.20
Then I set decimal(10,5) it show five decimal place with all number thought it not necessary like 10.00000.
I want to show decimal places only if have fraction
like 10.25455 = 10.25455 and 10 = 10 but it showing 10 = 10.00000
how to solve this
You can use PHP round function.
http://php.net/manual/en/function.round.php
PHP Code:
<?php
echo round(10.25455, 5); // outputs: 10.25455
echo '<br/>';
echo round(10.00000, 5); // outputs: 10
Just add 0!
echo 10.25455 + 0;
echo 10.00000 + 0;
Output:
10.25455
10
Use decimal as a field type. Then in PHP use round(); function.
There is no datatype in mysql that supports your requirement. In fact, that is not how computers work. A number is either an int or a float.
You might want to use varchar instead, and cast to the right data type in php, but it might cause performance overhead. It is indeed a weird data type requirement. If you must go this route, consider adding an extra column for flagging data type.
echo floatval($value);
will do the trick.
Related
$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 am having a problem where by when I try and generate numbers randomly with either mt_rand or rand (on a larger scale) I get no result at all. This used to work fine on my server but there seems to be issues now and I am unsure why.
<?php
echo 'Your number is: '.rand(0, 99999999999999999999);
?>
Where as it I update it to something like (using a 9 digit number):
<?php
echo 'Your number is: '.rand(0, 999999999);
?>
the lower example will work fine. I have recently changed my server version PHP 7.0. Is there a way to increase the maximum number or a better way to be doing this? Thanks.
On both 32-bit and 64-bit systems, the number 99999999999999999999 is too large to be represented as an integer in PHP 7, so it becomes a float.
However, mt_rand() takes two integers. Because 99999999999999999999 is too large to be an integer, when you pass it to the function, PHP 7 throws an error, because it cannot be safely converted. You didn't get this error for 999999999, because it's small enough to be an integer.
Anyway, your current code probably isn't doing that you want under PHP 5: 99999999999999999999 is being silently converted to 7766279631452241920 on 64-bit systems, and something else on 32-bit systems. So you're not getting the full range of random numbers. You wrote rand(0, 99999999999999999999), but you're actually getting rand(0, 7766279631452241920).
If you just want a random number from the widest possible range, try this:
<?php
echo 'Your number is: '.rand(0, PHP_INT_MAX);
?>
PHP_INT_MAX is a constant containing the largest possible integer (which will be different depending on whether you're on 32-bit or 64-bit). So, doing this will always give you the widest possible range of random positive numbers from that function.
To run the code in php 7 you have to typecast
<?php
echo 'Your number is: '.rand(0,(int) 99999999999999999999);
?>
Microtime generated random everytime.
function microtimeRand( $min, $max ) {
$microtimeInt = intval( microtime( true ) * 100 );
$microtimeInt = $microtimeInt % ($max - $min);
$microtimeInt += $min;
return $microtimeInt;
}
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
Yesterday I was helping some one and got a weird error which I could not explain to him how it worked.
The code (tested on 3 machines (PHP 5.3 and PHP 5.4))
// (float)65.35
$percentage = round(65.351, 2);
// (float) 6535
$total = $percentage * 100;
// (int) 6534
$int = (int) $total;
What is suspected was that the int value would be 6535 but it ended up being 6534.
Could some one explain what is going on?
You don't actually have 65.35 after the first operation.
>>> '%.20f' % (65.351 - 0.001,)
'65.34999999999999431566'
Either start with an integral value scaled appropriately in the first place, don't attempt to convert the value to an integer, or add a small value before taking the integer value.
This has to do with how floating point (read the warning in this link!) values are stored in memory. Indeed after the first operation you don't have an exact decimal value, but a rounded value. Probably 65.34999999 or so. (The value is stored as a list of bits (0/1))
This is why when talking about money, developers don't store dollars/euros but rather the amount of cents. This way they avoid working with floats that are less precise for decimals, but rather work with integers, that are precise.
Use round instead of int
round($total)
$r=$explode('.',$total);
debug($r);
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
?>